AIM:
To
write a C program to perform the addition of two given polynomials using
linked list.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the structure with
two data fields; one coefficient field, one
exponent field and one pointer to point the next element in
the linked list.
Step
3: Get the coefficients and exponents of polynomials as input.
Step 4: For the given polynomials create node
for each term using malloc and
link each term with the next term
of the same polynomial.
Step
5: Point last term of each polynomial to NULL.
Step
6: Compare the exponent terms of the polynomials one by one.
i) If they are same add the respective coefficients and then
create node and
place them in the resultant polynomial
ii) Else if they do not match with any term then place the term as such
in the resultant polynomial.
Step 7: Display the resultant polynomial
Step 8: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
#include<stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}p;
void main()
{
p *p1,*p2,*p3;
p *getpoly();
p *add(p*,p*);
void display(p*);
clrscr();
printf("\n Enter the 1st polynomial:");
p1=getpoly();
clrscr();
printf("\n Enter the 2nd polynomial:");
p2=getpoly();
clrscr();
printf("\n The 1st poly is:");
display(p1);
printf("\n The 2nd poly is:");
display(p2);
p3=add(p1,p2);
printf("\nAddition of polynomial is:");
display(p3);
exit(0);
}
p *getpoly()
{
p *temp,*New,*last;
int Exp,flag;
float Coef;
p *getnode();
char ans='y';
temp=NULL;
flag=1;
printf("\n Enter the polynomial in descending order of
exponent:");
do
{
printf("\nEnter the coeff and expo of a term:");
scanf("%f%d",&Coef,&Exp);
New=getnode();
if(New==NULL)
printf("\n Memory not allocated:");
New->coef=Coef;
New->exp=Exp;
if(flag==1)
{
temp=New;
last=temp;
flag=0;
}
else
{
last->next=New;
last=New;
}
printf("\nDo you want to add more terms(y/n)?");
ans=getch();
}while(ans=='y');
return(temp);
}
p *getnode()
{
p *temp;
temp=(p*)malloc(sizeof(p));
temp->next=NULL;
return(temp);
}
void display(p *head)
{
p *temp;
temp=head;
if(temp==NULL)
{
printf("\n The polynomial is empty");
getch();
return;
}
printf("\n");
while(temp->next!=NULL)
{
printf("%0.1fx^%d+",temp->coef,temp->exp);
temp=temp->next;
}
printf("%0.1fx^%d",temp->coef,temp->exp);
getch();
}
p *add(p* one,p* two)
{
p *p1,*p2,*temp,*dummy;
char ch;
float Coef;
p *append(int,float,p*);
p1=one;
p2=two;
temp=(p*)malloc(sizeof(p));
if(temp==NULL)
printf("\nMemory not allocated");
dummy=temp;
while(p1!=NULL&&p2!=NULL)
{
if(p1->exp==p2->exp)
{
Coef=p1->coef+p2->coef;
temp=append(p1->exp,Coef,temp);
p1=p1->next;
p2=p2->next;
}
else if(p1->exp<p2->exp)
{
Coef=p2->coef;
temp=append(p2->exp,Coef,temp);
p2=p2->next;
}
else if(p1->exp>p2->exp)
{
Coef=p1->coef;
temp=append(p1->exp,Coef,temp);
p1=p1->next;
}}
while(p1!=NULL)
{
temp=append(p1->exp,p1->coef,temp);
p1=p1->next;
}
while(p2!=NULL)
{
temp=append(p2->exp,p2->coef,temp);
p2=p2->next;
}
temp->next=NULL;
temp=dummy->next;
free(dummy);
return(temp);
}
p *append(int Exp,float Coef,p *temp)
{
p *New,*dummy;
New=(p*)malloc(sizeof(p));
if(New==NULL)
printf("\nMemory not alloated\n");
New->exp=Exp;
New->coef=Coef;
New->next=NULL;
dummy=temp;
dummy->next=New;
dummy=New;
return(dummy);
}
OUTPUT
Enter the 1st polynomial:
Enter the polynomial in descending order of exponent:
Enter the coeff and expo of a term:3 1
Do you want to add more terms(y/n)?
Enter the coeff and expo of a term:4 0
Do you want to add more terms(y/n)?
Enter the 2nd polynomial:
Enter the polynomial in descending order of exponent:
Enter the coeff and expo of a term:4 1
Do you want to add more terms(y/n)?
Enter the coeff and expo of a term:6 0
Do you want to add more terms(y/n)?
The 1st poly is:
3.0x^1+4.0x^0
The 2nd poly is:
4.0x^1+6.0x^0
Addition of polynomial is:
7.0x^1+10.0x^0
RESULT: Thus a C program is written to implement polynomial
addition using linked
list and executed successfully
No comments:
Post a Comment