0% found this document useful (0 votes)
22 views4 pages

DC Codes

The document contains a C program that defines a structure for polynomial representation and provides functions to read, multiply, and add polynomials. It allows users to input two polynomials and displays the results of their multiplication and addition. The program utilizes linked lists to manage polynomial terms, including coefficients and exponents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

DC Codes

The document contains a C program that defines a structure for polynomial representation and provides functions to read, multiply, and add polynomials. It allows users to input two polynomials and displays the results of their multiplication and addition. The program utilizes linked lists to manage polynomial terms, including coefficients and exponents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File: /home/shahana/d.

c Page 1 of 4

#include<stdio.h>
#include<stdlib.h>

struct Node{
int coef;
int exp;
struct Node *link;
};

struct Node *Phead, *Qhead, *Rhead, *new, *ptr;

struct Node *ReadPoly()


{
struct Node *link, *ptr, *head=NULL;
int n,i;
printf("enter the total no of terms in polynomial:");
scanf("%d",&n);
printf("enter the coef and exp in polynomial:");
for(i=1;i<=n;i++)
{
printf("enter the coef(%d), exp(%d)",i,i);
new = (struct Node *) malloc(sizeof(struct Node));
scanf("%d",&new->coef);
scanf("%d",&new->exp);
new->link=NULL;
if(head==NULL)
{
head=new;
ptr=head;
}
else
{
ptr->link=new;
ptr=new;
}

}
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);
}

void DisplayPoly(struct Node *head)


{
struct Node *ptr;
if(head==NULL)
{
printf("polynomial is empty");
}
else
{
ptr=head;
while(ptr->link!=NULL)
{
printf("%d x^%d +",ptr->coef,ptr->exp);
ptr=ptr->link;
}
printf("%d x^%d\t",ptr->coef,ptr->exp);
}
}

struct Node *AddPoly()


{
struct Node *temp, *P, *Q, *R, *prev, *head=NULL;
P=Phead;
Q=Qhead;
while(P!=NULL && Q!=NULL)
{
if(P->exp==Q->exp)
{
new = (struct Node *) malloc(sizeof(struct Node));
new->coef=P->coef+Q->coef;
new->exp=P->exp;
new->link=NULL;
P=P->link;
Q=Q->link;
}
else if(P->exp>Q->exp)
File: /home/shahana/d.c Page 3 of 4

{
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("enter the details of polynomial 2:");


Qhead=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);

You might also like