Given two polynomial numbers represented by a linked list. Write a function that add these lists means add the coefficients who have same variable powers.
Linked List : It is a data structure that stores each element as an object in a node of the list. every node contains three parts two integer part for coefficient(c), exponent(e) and one for next pointer.
A linked list that is used to store Polynomial looks like −
Polynomial : 4x7 + 12x2 + 45
This is how a linked list represented polynomial looks like.
Adding two polynomials that are represented by a linked list. We check values at the exponent value of the node. For the same values of exponent, we will add the coefficients.
Example,
Input :
p1= 13x8 + 7x5 + 32x2 + 54
p2= 3x12 + 17x5 + 3x3 + 98
Output : 3x12 + 13x8 + 24x5 + 3x3 + 32x2 + 152
Check out Code Here 👇
// Author : SPPU CSE GURU | |
#include<iostream> | |
using namespace std; | |
struct pterm | |
{ | |
int c; | |
int e; | |
pterm *next; | |
}; | |
class Poly{ | |
public: | |
pterm *head,*tail,*nterm; | |
Poly(){ | |
head=tail=NULL; | |
} | |
void create(); | |
void Display(); | |
void PolyAdd(Poly,Poly); | |
}; | |
void Poly::create(){ | |
int t; | |
cout<<"How many terms are there in polynomial ?"<<endl; | |
cin>>t; | |
cout<<"Enter Polynomial terms in decending order"<<endl; | |
for(int i=1;i<=t;i++){ | |
nterm=new pterm; | |
nterm->next=NULL; | |
cout<<"Enter "<<i<<" Coeffient and its exponant"<<endl; | |
cin>>nterm->c>>nterm->e; | |
if(head==NULL){ | |
head=tail=nterm; | |
} | |
else{ | |
tail->next=nterm; | |
tail=nterm; | |
} | |
} | |
} | |
void Poly::PolyAdd(Poly obj1,Poly obj2){ | |
pterm *temp1,*temp2,*res; | |
temp1=obj1.head; | |
temp2=obj2.head; | |
while(temp1!=NULL && temp2!=NULL){ | |
res=new pterm; | |
res->next=NULL; | |
if(temp1->e == temp2->e){ | |
res->c=(temp1->c)+(temp2->c); | |
res->e=temp1->e; | |
temp1=temp1->next; | |
temp2=temp2->next; | |
} | |
else if(temp1->e > temp2->e){ | |
res->c=temp1->c; | |
res->e=temp1->e; | |
temp1=temp1->next; | |
} | |
else{ | |
res->c=temp2->c; | |
res->e=temp2->e; | |
temp2=temp2->next; | |
} | |
if(head==NULL){ | |
head=tail=res; | |
} | |
else{ | |
tail->next=res; | |
tail=res; | |
} | |
} | |
while(temp1!=NULL){ | |
res=new pterm; | |
res->next=NULL; | |
res->c=temp1->c; | |
res->e=temp1->e; | |
temp1=temp1->next; | |
if(head==NULL){ | |
head=tail=res; | |
} | |
else{ | |
tail->next=res; | |
tail=res; | |
} | |
} | |
while(temp2!=NULL){ | |
res=new pterm; | |
res->next=NULL; | |
res->c=temp2->c; | |
res->e=temp2->e; | |
temp2=temp2->next; | |
if(head==NULL){ | |
head=tail=res; | |
} | |
else{ | |
tail->next=res; | |
tail=res; | |
} | |
} | |
} | |
void Poly::Display(){ | |
pterm *temp; | |
temp=head; | |
//cout<<"Exp "; | |
while(temp!=NULL){ | |
if(temp==tail){ | |
cout<<temp->c<<"x^"<<temp->e; | |
temp=temp->next; | |
} | |
else{ | |
cout<<temp->c<<"x^"<<temp->e<<"+"; | |
temp=temp->next; | |
} | |
} | |
cout<<"\n"; | |
} | |
int main(){ | |
Poly obj1,obj2,Ans; | |
obj1.create(); | |
obj2.create(); | |
cout<<"First Polynomial : "; | |
obj1.Display(); | |
cout<<"\n"; | |
cout<<"Second Polynomial : "; | |
obj2.Display(); | |
cout<<"\n"; | |
cout<<"Addition is : "; | |
Ans.PolyAdd(obj1,obj2); | |
Ans.Display(); | |
return 0; | |
} |
Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊
Comments
Post a Comment