Polynomial Addition:
Linked lists are widely used to represent and manipulate polynomials, polynomials are the expression
containing number of terms with nonzero coefficients and exponents. In the linked list representation of
polynomials, each term is considered as node. And such a node contains three fields:
1. Coefficient field-> holds the value of the coefficient of a term.
2. Exponent field-> holds the exponent value to that term.
3. Linked field-> holds the address of the next term of the polynomial.
The logical representation of the above node is given below:
typedef struct poly
{
int cof;
int power;
struct poly *next;
}pnode;
The steps involved in adding two polynomials are given below:
1. Read the coefficient and exponents of the first polynomial.
2. Read the coefficient and exponents of the second polynomial.
3. Set the temporary pointers p and q to traverse the two polynomials respectively.
4. Compare the exponents of two polynomials starting from the first nodes.
If both exponents are equal then add the coefficients and store it in the resultant linked list.
If the exponent of the current term in the first polynomial p is less than the exponent of the current
term of the second polynomial is added to the resultant linked list. And move the pointer q to point
to the next node in the second polynomial q.
If the exponent of the current term in the first polynomial p is greater than the exponent of the
current term in the second polynomial q, then the current term of the first polynomial is added to the
resultant linked list. And move the pointer p to the next node.
Append the remaining nodes of either of the polynomials to the resultant linked list.
void createpoly(pnode *list)
{
printf(“\nEnter Coefficient:”);
scanf(“%d”, &list->cof);
printf(“\n Enter Power:”);
scanf(“%d”,&list->power);
print(“\n Another Term(y \ n):”);
fflush(stdin);
ch=getch();
ff(ch==„n‟)
{
list->next=NULL;
return;
}
list->next=(pnode *)malloc(xizeof(pnode));
createpoly(list->next);
}
void polynomialadd()
{
pnode *p1,*p2*p3,*phead;
int newcof=0,newpower=0;
p3=(pnode *)calloc(1,sizeof(pnode));
phead=p3;
p1=(pnode *)malloc(sizeof(pnode));
Data Structure & Algorithm, Dr. K. Bhowal
p2=(pnode *)malloc(sizeof(pnode));
printf(“\n Enter Coefficient and Power for Polynomial 1:”);
createpoly(p1);
printf(“\n Enter Coefficient and Power for Polynomial 2:”);
createpoly(p2);
printf(“\n First Polynomial:”);
printpoly(p1);
printf(“\n Second Polynomial):
printpoly(p2);
while(p1!=NULL &&p2!=NULL)
{
if(p1->power==p2->power)
{
newcof=p1->cof+p2->cof;
newpower=p1->power;
p1=p1->next;
p2=p2->next;
}
else
{
if(p1->power>p2->power)
{
newcof=p1->cof;
newpower=p1->power;
p1=p1->next;
}
else
{
if(p1->power<p2->power)
{
newfof=p2->cof;
newpower=p2->power;
p2=p2->next;
}
}
}
if(newcof!=0)
{
p3->cof=newcof;
p3->power=newpower;
p3->next=(pnode *)calloc(1,sizeof(pnode));
p3=p3->next;
newcof=0;
}
}
if(p1==NULL)
{
while((p2!=NULL)
{
p3->cof=p2->cof;
p3->power=p2->power;
p3->next=(pnoe *)calloc(1,sizeof(pnode));
p3=p3->next;
p2=p2->next;
}
}
else
{
while(p1!=NULL)
{ p3->cof=p1->cof;
p3->power=p1->power;
p3->next=(pnode *)calloc(1,sizeof(pnode));
Data Structure & Algorithm, Dr. K. Bhowal
p3=p3->next;
p1=p1->next;
}
}
p3=NULL;
printf(“\n Resultant Polynomial:”);
printpoly(phead);
}
void printpoly(pnode *list)
{ printf(“%d %d “,list->cof,list->power);
if(list->next==NULL)
return;
else
printpoly(list->next);
}
Data Structure & Algorithm, Dr. K. Bhowal