954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help in linked list polynomial addition

#include
using namespace std;
class polynomial
{
private:

struct p
{
int degree;
float coefficient;
p *link;

}*head;


public:
polynomial()
{
head=NULL;
}
void getdegree();
void getpolynomial(int d);
void display();
void addition(polynomial p1,polynomial p2);
};
void polynomial::getdegree()
{
int degree1;
cout<<"\n Enter the highest degree of your polynomial:";
cin>>degree1;
getpolynomial(degree1);
}
void polynomial::getpolynomial(int degree1)
{
int i=0;
p *temp,*data;
float *arr;
arr=new float[degree1+1];
for(i=degree1;i>=0;i--)
{
if(i==0)
{
cout<<"\n Enter the constant term:";
cin>>arr[i];
}
else
{
cout<<"\n Enter the coefficient for degree x^"<>arr[i];
}

}
for(i=degree1;i>=0;i--)
{

if(head==NULL)
{
head=new p;
head->coefficient=arr[i];
head->degree=i;
head->link=NULL;
}
else
{
temp=head;
while(temp->link!=NULL)
temp=temp->link;
data=new p;
data->coefficient=arr[i];
data->degree=i;
data->link=NULL;
temp->link=data;
}
}
}
void polynomial::display()
{
p *temp;
temp=head;
while(temp!=NULL)
{
if(temp->coefficient>=0)
{
cout<<"+"<coefficient;

}
else
cout<coefficient;
if(temp->degree!=0)
{
cout<<"x^"<degree;
cout<<" ";
}

temp=temp->link;
}
}
void polynomial::addition(polynomial p1,polynomial p2)
{
float coeff;
int deg;
p *temp1,*temp2,*temp3,*node;
temp1=p1.head;
temp2=p2.head;
temp3=head;
while(temp1&&temp2)
{
if(temp1->degree>temp2->degree)
{
coeff=temp1->coefficient;
deg=temp1->degree;
temp1=temp1->link;
}

else if(temp1->degreedegree)
{
coeff=temp2->coefficient;
deg=temp2->degree;
temp2=temp2->link;
}
else
{
coeff=temp1->coefficient+temp2->coefficient;
deg=temp2->degree;
temp1=temp1->link;
temp2=temp2->link;
}
if(temp3==NULL)
{
temp3->coefficient=coeff;
temp3->degree=deg;
temp3->link=NULL;
}
else
{
node=new p;
node->coefficient=coeff;
node->degree=deg;
node->link=NULL;
temp3->link=node;
temp3=temp3->link;

}

}
}

main()
{
polynomial p1,p2,p3;
p1.getdegree();
p1.display();
p2.getdegree();
p2.display();
p3.addition(p1,p2);
p3.display();
}

this is my code for polynomial addition using linked list.It reads the first two polynomials and displays it correctly but it doesnt add them and display it...can someone help me in correcting this code...i would be glad if someone provides codes for the addition function,,:-)

arunkumar267
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

#include using namespace std; class polynomial { private:

struct p { int degree; float coefficient; p *link;

}*head;

public: polynomial() { head=NULL; } void getdegree(); void getpolynomial(int d); void display(); void addition(polynomial p1,polynomial p2); }; void polynomial::getdegree() { int degree1; cout<<"\n Enter the highest degree of your polynomial:"; cin>>degree1; getpolynomial(degree1); } void polynomial::getpolynomial(int degree1) { int i=0; p *temp,*data; float *arr; arr=new float[degree1+1]; for(i=degree1;i>=0;i--) { if(i==0) { cout<<"\n Enter the constant term:"; cin>>arr[i]; } else { cout<<"\n Enter the coefficient for degree x^"<>arr[i]; }

} for(i=degree1;i>=0;i--) {

if(head==NULL) { head=new p; head->coefficient=arr[i]; head->degree=i; head->link=NULL; } else { temp=head; while(temp->link!=NULL) temp=temp->link; data=new p; data->coefficient=arr[i]; data->degree=i; data->link=NULL; temp->link=data; } } } void polynomial::display() { p *temp; temp=head; while(temp!=NULL) { if(temp->coefficient>=0) { cout<<"+"<coefficient;

} else cout<coefficient; if(temp->degree!=0) { cout<<"x^"<degree; cout<<" "; }

temp=temp->link; } } void polynomial::addition(polynomial p1,polynomial p2) { float coeff; int deg; p *temp1,*temp2,*temp3,*node; temp1=p1.head; temp2=p2.head; temp3=head; while(temp1&&temp2) { if(temp1->degree>temp2->degree) { coeff=temp1->coefficient; deg=temp1->degree; temp1=temp1->link; }

else if(temp1->degreedegree) { coeff=temp2->coefficient; deg=temp2->degree; temp2=temp2->link; } else { coeff=temp1->coefficient+temp2->coefficient; deg=temp2->degree; temp1=temp1->link; temp2=temp2->link; } if(temp3==NULL) { temp3->coefficient=coeff; temp3->degree=deg; temp3->link=NULL; } else { node=new p; node->coefficient=coeff; node->degree=deg; node->link=NULL; temp3->link=node; temp3=temp3->link;

}

} }

main() { polynomial p1,p2,p3; p1.getdegree(); p1.display(); p2.getdegree(); p2.display(); p3.addition(p1,p2); p3.display(); }

this is my code for polynomial addition using linked list.It reads the first two polynomials and displays it correctly but it doesnt add them and display it...can someone help me in correcting this code...i would be glad if someone provides codes for the addition function,,:-)


its ok i've solved it :-)

arunkumar267
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: