DC Codes
DC Codes
c Page 1 of 4
#include<stdio.h>
#include<stdlib.h>
struct Node{
int coef;
int exp;
struct Node *link;
};
}
return(head);
}
struct Node *MulPoly()
{
struct Node *temp, *P, *Q, *R, *prev, *head=NULL;
P=Phead;
Q=Qhead;
while(P!=NULL)
{while(Q!=NULL)
{
new = (struct Node *) malloc(sizeof(struct Node));
new->coef=P->coef*Q->coef;
new->exp=P->exp+Q->exp;
new->link=NULL;
if(head==NULL)
{
head=new;
R=head;
}
else
{
R->link=new;
R=new;
}
File: /home/shahana/d.c Page 2 of 4
Q=Q->link;
}
P=P->link;
Q=Qhead;
}
P=head;
while(P!=NULL)
{
prev=P;
P=P->link;
while(Q!=NULL)
{
if(P->exp==Q->exp)
{
P->coef;
prev->link=Q->link;
free(Q);
Q=prev->link;
}
else
{
prev=Q;
Q=Q->link;
}
P=P->link;
}
}
return(head);
}
{
new = (struct Node *) malloc(sizeof(struct Node));
new->coef=P->coef;
new->exp=P->exp;
new->link=NULL;
P=P->link;
}
else
{
new = (struct Node *) malloc(sizeof(struct Node));
new->coef=Q->coef;
new->exp=Q->exp;
new->link=NULL;
Q=Q->link;
}
if(head==NULL)
{
head=new;
R=head;
}
else
{
R->link=new;
R=new;
}
}
while(P!=NULL)
{
new = (struct Node *) malloc(sizeof(struct Node));
new->coef=P->coef;
new->exp=P->exp;
new->link=NULL;
if(head==NULL)
{
head=new;
R=head;
}
else
{
R->link=new;
R=new;
}
Q=Q->link;
}
return(head);
}
void main()
{
printf("enter the details of polynomial 1:");
Phead=ReadPoly();
printf("\nfirst polynomial:");
DisplayPoly(Phead);
printf("\nsecond polynomial:");
DisplayPoly(Qhead);
Rhead=MulPoly();
printf("\nresulltant poly product:");
File: /home/shahana/d.c Page 4 of 4
DisplayPoly(Rhead);
Rhead=AddPoly();
printf("\nresulltant poly addition:");
DisplayPoly(Rhead);