You are on page 1of 4

WAP TO ADD,APPEND & DISPLAY POLYNOMIALS USING

LINKED LIST
#include<conio.h>
#include<stdio.h>
struct polynode
{
float coefficient;
int expo;
struct polynode *link;
};
struct polynode *first *sec *add;
void addpoly() //FUNCTION TO ADD TWO POLYNOMIALS
{
struct polynode *x *y *z;
x=first;
y=second;
z=add;
If(x==NULL&&y==NULL)
{
z=NULL;
return;
}
while(x!=NULL && y!=NULL)
{
if(z=NULL)
{
z=mallloc(sizeof(struct polynode));
ad=z;
}
else
{
z->link=malloc(sizeof(struct polynode));
}
If(x->expo>y->expo)
{
z->coeff=x->coeff;
z->expo=x->expo;
x=x->link;
}
else if(x->expo<y->expo)
{
z->coeff=y->coeff;
z->expo=y->expo;
y=y->link;
}
else if(x->expo==y->expo)
{
z->coeff=x->coeff+y->coeff;
z->expo=x->expo;
x=x->link;
y=y->link
}
}

void append(int coeff1,int exp,int i) //FUNCTION TO ADD AN ELEMENT IN A


POLYNOMIAL
{
struct polynode *temp,*trav;
if(i==1)
trav=first;
else
trav=second;
switch(i)
{
case 1:
if(first ==NULL)
first=temp;
else
{
while(trav->link!=NULL)
{
if(trav->expo==expo1)
trav->coeff+=coeff1;
else
{
if(trav->next->expo<expo1)
{
temp->link=trav->link;
trav->link=temp;
}
}
trav=trav->link;
}
}
break;
case2:
if(second==NULL)
second=temp;
else
{
while(trav->link!=NULL)
{
if(trav->expo==expo1)
trav->coeff+=coeff1;
else
{
if(trav->next->expo<expo1)
{
temp->link=trav->link;
trav->link=temp;
}
}

trav=trav->link;
}
}
break;
default:
printf(“\n WRONG CHOICE ENTERED.”);
};
}

void display() //FUNCTION TO DISPLAY ADDITION OF POLYNOMIALS


{
struct polynode *trav;
trav=add;
while(trav!=NULL)
{
printf(“\t%d”,trav->info);
trav=trav->next;
}
}

void main()
{
first=NULL;
sec=NULL;
add=NULL;
int q,a,b,c,d;
start:
clrscr();
printf(“\n TO APPEND PRESS 1.”);
printf(“\n TO ADD PRESS 2.”);
printf(“\n TO DISPLAY PRESS 3.”);
printf(“\n ENTER YOUR CHOICE.”);
scanf(“%d,&q);
switch(q)
{
case1:
printf(“\n ENTER THE COEFFICIENT-:”);
scanf(“%d”,&a);
printf(“\n ENTER THE EXPONENT-:”);
scanf(“%d”,&b);
printf(“\n ENTER THE POLYNOMIAL TO WHICH YOU WISH TO APPEND-:”);
scanf(“%d”,&c);
append(a,b,c);
break;
case2:
addpolynode();
break;
case3:
display();
break;
default:
printf(“\n WRONG CHOICE ENTERED.”);
};
printf(“\n WISH TO CONTINUE(1/0).”);
scanf(“%d”,&d);
if(d==1)
goto start;
getch();
}

OUTPUT

TO APPEND PRESS 1.
TO ADD PRESS 2.
TO DISPLAY PRESS 3.
ENTER YOUR CHOICE.-:1
ENTER THE COEFFICIENT-:2
ENTER THE EXPONENT-:2
ENTER THE POLYNOMIAL TO WHICH YOU WISH TO APPEND-:1
WISH TO CONTINUE(1/0).-: