```
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
signed int coe;
int pow;
node *next;
}*start1,*start2,*root,*add_res,*mul_res;
void disp(node *cur)
{
while(cur!=NULL)
{
printf("%dx%d ",cur->coe,cur->pow);
cur=cur->next;
}
}
void ins(node **root,int c,int p)
{
node *temp=new node;
node *cur=new node;
cur=*root;
temp->coe=c;
temp->pow=p;
temp->next=NULL;
int flag=0;
if(*root==NULL)
{
*root=temp;
}
else
{
//flag=0 for ordinary insertion
//flag=1 for multiplication
//without flag, the result will be 2x2 + 3x2
// and not 5x2(co-eff wont get added for same powers)
while(cur->next!=NULL && flag==0)
{
if(p==cur->pow)
{
cur->coe=cur->coe + c;
flag=1;
}
cur=cur->next;
}
if(p==cur->pow && flag==0) // this is used for multiplication
cur->coe=cur->coe + c; //addition wont pass into this
else if(flag==0) //condition
cur->next=temp;
}
}
void create()
{
char ch;
int c,p;
for(int i=1;i<=2;i++)
{
printf("Enter for polynomial %d (decreasing power order)",i);
ch='c';
// press s after giving i/p
while(ch!='s')
{
printf("\nCo-eff (space) Power :");
scanf("%d %d",&c,&p);
if(i==1)
ins(&start1,c,p);
else if(i==2)
ins(&start2,c,p);
scanf("%s",&ch);
}
root=NULL;
}
printf("Poly 1 \n");
disp(start1);
printf("Poly 2 \n");
disp(start2);
}
void mul()
{
node *cur1=start1,*cur2=start2;
while(cur2!=NULL)
{
while(cur1!=NULL)
{
//multiply co-eff and add the powers
ins(&mul_res,cur1->coe * cur2->coe,cur1->pow + cur2->pow);
cur1=cur1->next;
}
cur2=cur2->next;
cur1=start1;
}
printf("\n\nThe MUL res is \n");
disp(mul_res);
}
void add()
{
node *cur1=start1;
node *cur2=start2;
while(cur1!=NULL && cur2!=NULL)
{
int k;
if(cur1->pow == cur2->pow)
{
k=cur1->coe + cur2->coe;
ins(&add_res,k,cur1->pow);
cur1=cur1->next;
cur2=cur2->next;
}
else if(cur1->pow > cur2->pow)
{
ins(&add_res,cur1->coe,cur1->pow);
cur1=cur1->next;
}
else if(cur2->pow > cur1->pow)
{
ins(&add_res,cur2->coe,cur2->pow);
cur2=cur2->next;
}
}
// insert the remaining elements of cur2
if(cur1==NULL)
{
while(cur2!=NULL)
{
ins(&add_res,cur2->coe,cur2->pow);
cur2=cur2->next;
}
}
// insert the remaining elements of cur1
else
while(cur1!=NULL)
{
ins(&add_res,cur1->coe,cur1->pow);
cur1=cur1->next;
}
printf("\n\nThe ADD res is \n");
disp(add_res);
}
void main()
{
start1=NULL;
start2=NULL;
add_res=NULL;
mul_res=NULL;
clrscr();
create();
add();
mul();
getch();
}
```