#include<iostream>
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^"<<i<<":";
           cin>>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<<"+"<<temp->coefficient;

         }
         else
           cout<<temp->coefficient;
           if(temp->degree!=0)
           {
             cout<<"x^"<<temp->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->degree<temp2->degree)
           {
               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 :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.