Updated:13/01/2024 by Computer Hope
Go BackGiven two polynomial numbers represented by a linked list.
An example of a polynomial is 6x2+9x+10; here, the total number of terms is 3. The coefficients of each term are 6, 9, 10 and degrees 2, 1, 0 respectively.
Another polynomial is 5x2+7x+10; here, the total number of terms is 3. The coefficients of each term are 7, 5, 10 and degrees 2, 1, 0 respectively
Now we are adding this two polynomial with this program.
The addition operation will involve adding like terms and maintaining the result as a new linked list.
Write a function that add these lists.
Want to learn how we can implement Reprent polynomials and addition
//WAP to Reprent polynomials and addition
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{
int coef,exp;
struct node *next;
};
struct node *s1=NULL,*s2=NULL,*s3=NULL;
void append(struct node *&p,int a,int b)
{
struct node *q=p,*r;
r=(struct node *)malloc(sizeof(struct node));
r->coef=a;
r->exp=b;
if(p==NULL)
{p=r;
p->next=NULL;
return; }
if(r->exp > p->exp)
{ r->next=p;
p=r;
return; }
while(q->next != NULL)
{if(q->exp==r->exp)
{q->coef+=r->coef;
free(r);
return; }
if(q->next!=NULL && q->exp > r->exp && r->exp > q->next->exp)
{ r->next=q->next;
q->next=r;
return;}
q=q->next;
}
if( q->exp > r->exp && q->next==NULL )
{ r->next=q->next;
q->next=r;
return; }
}
void show(struct node *p)
{if(p==NULL)
{ printf("\n no polynomial");
return; }
while(p!=NULL)
{ printf("%dX%d ",p->coef,p->exp);
p=p->next; }
}
void sum()
{ struct node *p=s1,*q=s2;
while(p!=NULL)
{ append(s3,p->coef,p->exp);
p=p->next; }
while(q!=NULL)
{ append(s3,q->coef,q->exp);
q=q->next; }
printf("\n Addition of two polynomials ");
show(s3);
}
void main()
{ int ch, e,c;
do{
printf("\n 1 Append (Polynommial s1 ):");
printf("\n 2 Append (Polynommial s2 ):");
printf("\n 3 Show ( Polynomial s1 ):");
printf("\n 4 Show ( Polynomial s2 ):");
printf("\n 5 Sum ( s1 + s2 ):");
printf("\n 0 Exit ");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{ case 1:printf("\n enter coef and exponent ");
scanf("%d%d",&c,&e);
append(s1,c,e);
break;
case 2:printf("\n enter coef and exponent ");
scanf("%d%d",&c,&e);
append(s2,c,e);
break;
case 3:show(s1);
break;
case 4:show(s2);
break;
case 5:sum();
break;
case 0:break;
default:printf("\n Invalid Choice ");
}
}while(ch != 0);
}