Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int exp;
- int coeff;
- struct node *next;
- };
- struct node *ReadPoly();
- struct node *AddPoly(struct node *p1,struct node *p2);
- struct node *MultiplyPoly(struct node *p1,struct node *p2);
- void Display(struct node *head);
- int main()
- {
- struct node *Phead,*Qhead,*Rhead;
- Phead=Qhead=Rhead=NULL;
- int c;
- printf("***FIRST POLYNOMIAL***");
- Phead=ReadPoly();
- printf("\nThe Inputted Polynomial 1 is: \n");
- Display(Phead);
- printf("\n");
- printf("\n***SECOND POLYNOMIAL***");
- Qhead=ReadPoly();
- printf("\nThe Inputted Polynomial 2 is: \n");
- Display(Qhead);
- printf("\n");
- do
- {
- printf("\n***LINKED LIST OPERATIONS***\n");
- printf("1.ADD POLYNOMIAL\n2.MULTIPLY POLYNOMIAL\n3.EXIT");
- printf("\nEnter your choice: ");
- scanf("%d",&c);
- switch(c)
- {
- case 1:
- Rhead=AddPoly(Phead,Qhead);
- printf("\nResultant Polynomial is: \n");
- Display(Rhead);
- break;
- case 2:
- Rhead=MultiplyPoly(Phead,Qhead);
- printf("Resultant Polynomial is: \n");
- Display(Rhead);
- break;
- case 3:
- printf("EXIT");
- break;
- default:
- printf("Enter valid choice(1,2,3)");
- break;
- }
- }while(c!=3);
- }
- struct node *ReadPoly()
- {
- int terms,i,c,e;
- struct node *n,*head=NULL,*ptr;
- printf("\nEnter the number of terms: ");
- scanf("%d",&terms);
- for(i=0;i<terms;i++)
- {
- n=(struct node*)malloc(sizeof(struct node));
- printf("\nEnter the coefficient (%d): ",(i+1));
- scanf("%d",&c);
- printf("\nEnter the exponent (%d): ",(i+1));
- scanf("%d",&e);
- n->coeff=c;
- n->exp=e;
- n->next=NULL;
- if(head==NULL)
- {
- head=ptr=n;
- }
- else
- {
- ptr->next=n;
- ptr=n;
- }
- ptr->next=NULL;
- }
- return head;
- }
- void Display(struct node *head)
- {
- struct node *ptr;
- ptr=head;
- while(ptr->next!=NULL)
- {
- printf("%d(x^%d)+",ptr->coeff,ptr->exp);
- ptr=ptr->next;
- }
- printf("%d(x^%d)",ptr->coeff,ptr->exp);
- }
- struct node *AddPoly(struct node *p1,struct node *p2)
- {
- struct node *p,*q,*n,*head=NULL,*ptr;
- p=p1;
- q=p2;
- while(p!=NULL &&q!=NULL)
- {
- n=(struct node*)malloc(sizeof(struct node));
- n->next=NULL;
- if(p->exp==q->exp)
- {
- n->exp=p->exp;
- n->coeff=p->coeff + q->coeff;
- p=p->next;
- q=q->next;
- }
- else if(p->exp>q->exp)
- {
- n->exp=p->exp;
- n->coeff=p->coeff;
- p=p->next;
- }
- else
- {
- n->exp=q->exp;
- n->coeff=q->coeff;
- q=q->next;
- }
- if(head==NULL)
- {
- head=ptr=n;
- }
- else
- {
- ptr->next=n;
- ptr=n;
- }
- }
- while(p!=NULL)
- {
- n=(struct node*)malloc(sizeof(struct node));
- n->exp=p->exp;
- n->coeff=p->coeff;
- n->next=NULL;
- p=p->next;
- if(head==NULL)
- {
- head=ptr=n;
- }
- else
- {
- ptr->next=n;
- ptr=n;
- }
- }
- while(q!=NULL)
- {
- n=(struct node*)malloc(sizeof(struct node));
- n->exp=q->exp;
- n->coeff=q->coeff;
- n->next=NULL;
- q=q->next;
- if(head==NULL)
- {
- head=ptr=n;
- }
- else
- {
- ptr->next=n;
- ptr=n;
- }
- }
- return head;
- }
- struct node *MultiplyPoly(struct node *p1,struct node *p2)
- {
- struct node *p,*q,*r,*n,*head,*prev;
- p=p1;
- q=p2;
- head=NULL;
- while(p!=NULL)
- {
- while(q!=NULL)
- {
- n=(struct node*)malloc(sizeof(struct node));
- n->exp=p->exp + q->exp;
- n->coeff=p->coeff * q->coeff;
- n->next=NULL;
- if(head==NULL)
- {
- head=r=n;
- }
- else
- {
- r->next=n;
- r=n;
- }
- q=q->next;
- }
- p=p->next;
- q=p2;
- }
- p=head;
- while(p!=NULL)
- {
- prev=p;
- q=p->next;
- while(q!=NULL)
- {
- if(p->exp==q->exp)
- {
- p->coeff=p->coeff + q->coeff;
- prev->next=q->next;
- free(q);
- q=prev->next;
- }
- else
- {
- prev=q;
- q=q->next;
- }
- }
- p=p->next;
- }
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement