1#include <stdio.h>2#include <stdlib.h>3 4typedefstructpolynomial5 {6 intCoef;7 intexpn;8 structPolynomial *Next;9 }node;Ten OneNode *creat () A { -Node *p,*q,*h; - intExpn,coef; theH= (node*)malloc(sizeof(node)); -p=h; -printf"Enter your coef and expn\n"); -scanf"%d%d",&coef,&expn); + while(coef!=0) - { +Q= (node*)malloc(sizeof(node)); Aq->coef=coef;q->expn=expn; atp->next=Q; -p=Q; -scanf"%d%d",&coef,&expn); - } -p->next=NULL; - returnh; in } - to voidPrint (node *h) + { -Node *p; thep=h; * while(p->next!=NULL) $ {Panax NotoginsengP=p->Next; - if(p->next!=NULL) theprintf"%dx^%d+",p->coef,p->expn); + Else Aprintf"%dx^%d\n",p->coef,p->expn); the } + - } $ $Node *mul (node*ha,node*HB) - { -node*p1,*p2,*p,*h,*Q; theP1=ha;p2=HB; -H= (node*)malloc(sizeof(node));Wuyip=h; the while(p1->next!=NULL) - { WuP1=p1->Next; - while(p2->next!=NULL) About { $P2=p2->Next; -Q= (node*)malloc(sizeof(node)); -Q->coef=p1->coef*p2->Coef; -Q->expn=p1->expn+p2->expn; Ap->next=Q; +p=Q; the } -P2=HB; $ } thep->next=NULL; the returnh; the } the -Node *simp (node*HC) in { theNode *p,*q,*Set; the Set=HC; AboutP=hc->Next; the while(Set->next!=NULL) the { the Set=Set-Next; + while(p->next!=NULL) - { theq=p;BayiP=p->Next; the if(Set->expn==p->expn) the { - Set->coef+=p->Coef; -Q->next=p->Next; theq=p; theP=p->Next; the Free(q); the } - } thep=Set-Next; the } the returnHC;94 } the the intMain () the {98printf"Hello world!\n"); AboutNode *h1,*h2,*h3,*h4; -printf"The first one is coef/n");101h1=creat ();102 print (H1);103printf"The second one is expen/n");104H2=creat (); the print (H2);106printf"The third one is mul\n");107h3=Mul (H1,H2);108h4=Simp (H3);109 print (H4); the return 0;111}
"Data structure experiment" polynomial multiplication