You are on page 1of 3

#include<stdio.

h>
#include<conio.h>
struct node
{
int c;
int p;
struct node *link;
}*h1,*h2,*h3;

void add_rear(struct node *h)


{
int i=1;
while(i)
{
struct node *temp;
struct node *cur;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter c and p:");
scanf("%d %d",&temp->c,&temp->p);
cur=h;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
temp->link=NULL;
printf("1 to continue 0 to exit:");
scanf("%d",&i);
}
}
void display(struct node *h)
{
struct node *cur;
cur=h;
while(cur->link!=NULL)
{
cur=cur->link;
if(cur->c>=0)
printf("+%dX%d\t",cur->c,cur->p);
else
printf("%dX%d\t",cur->c,cur->p);

}
}
void append(int c,int p)
{
struct node *temp;
struct node *cur;
temp=(struct node *)malloc(sizeof(struct node));
temp->c=c;
temp->p=p;
cur=h3;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
temp->link=NULL;
}

void add(struct node *h1,struct node *h2)


{
struct node *a;
struct node *b;
a=h1->link;
b=h2->link;
while(a!=NULL || b!=NULL)
{
if(a->p==b->p)
{
append(a->c+b->c,a->p);
a=a->link;
b=b->link;
}
else if(a->p<b->p)
{
append(b->c,b->p);
b=b->link;
}
else
{
append(a->c,a->p);
a=a->link;
}
}
while(a!=NULL)
{
append(a->c,a->p);
a=a->link;
}
while(b!=NULL)
{
append(b->c,b->p);
b=b->link;
}
}

void main()
{
clrscr();
h1=(struct node*)malloc(sizeof(struct node));
h2=(struct node*)malloc(sizeof(struct node));
h3=(struct node*)malloc(sizeof(struct node));
h1->link=NULL;
h2->link=NULL;
h3->link=NULL;
printf("enter the first polynomial\n");
add_rear(h1);
printf("enter the second polynomial\n");
add_rear(h2);
printf("the first polynomial is\n:");
display(h1);
printf("\nthe second polynomial is\n:");
display(h2);
add(h1,h2);
printf("the sum of polynomials:\n");
display(h3);
getch();
}

You might also like