#include #include struct poly { int coeff,exp; struct poly *link; }*head1=NULL,*head2=NULL,*head3=NULL,*temp1,*temp2,*temp3,*newnode,*next,*prev,*temp; void create(int n, int flag) { int i=1; while(i<=n) { newnode=(struct poly*)malloc(sizeof(struct poly)); printf("\n Enter the coefficient and exponnent respectively -\n"); scanf("%d %d",&newnode->coeff,&newnode->exp); newnode->link=NULL; if(flag==1) { if(head1==NULL) { temp1=newnode; head1=newnode; } else { temp1->link=newnode; temp1=newnode; } } else { if(head2==NULL) { temp2=newnode; head2=newnode; } else { temp2->link=newnode; temp2=newnode; } } i++; } } void display(struct poly *t) { temp=t; while(temp!=NULL) { if(temp->exp) printf("%dx^%d",temp->coeff,temp->exp); else printf("%d",temp->coeff); if(temp->link!=NULL) printf(" + "); temp=temp->link; } } void multiply() { temp1=head1; while(temp1!=NULL) { temp2=head2; while(temp2!=NULL) { newnode=(struct poly*)malloc(sizeof(struct poly)); newnode->coeff=temp1->coeff*temp2->coeff; newnode->exp=temp1->exp+temp2->exp; newnode->link=NULL; if(head3==NULL) { head3=newnode; temp3=newnode; } else { temp3->link=newnode; temp3=newnode; } temp2=temp2->link; } temp1=temp1->link; } temp3=head3; while(temp3!=NULL) { prev=temp3; next=temp3->link; while(next!=NULL) { if(temp3->exp==next->exp) { temp3->coeff=temp3->coeff+next->coeff; prev->link=next->link; free(next); next=prev->link; } else { prev=next; next=next->link; } } temp3=temp3->link; } } void main() { int n1,n2; printf("\n Enter the number of terms in first poly: "); scanf("%d",&n1); printf("\n Enter the number of terms in second poly: "); scanf("%d",&n2); printf("\n Enter the first polynomial - - - \n"); create(n1,1); printf("\n Enter the second polynomial - - -\n"); create(n1,2); printf("\n The Stored data\n The first poly - "); display(head1); printf("\n The second poly - "); display(head2); printf("\n The prod poly - "); multiply(); display(head3); printf("\n\n"); }