Nov
28
2008

# C++ Program to Multiply two polynomials using linked list

C++ Program to Multiply two polynomials using linked list.

```/**************************************************************
Author: Arun Vishnu M V
Web: www.arunmvishnu.com
Description: C++ Program to Multiply two polynomials using linked list.
***************************************************************/
#include<conio.h>
#include<iostream.h>
#include<process.h>

//   Creating a NODE Structure
struct node
{
int coe,exp;        // data
struct node *next;  // link to next node and previous node
};

// Creating a class Polynomial
class polynomial
{
struct node *start,*ptrn,*ptrp;
public:
void get_poly(); // to get a polynomial
void show();     // show
void multiply(polynomial p1,polynomial p2); // Multiply polynomials
void sort();  // Sort Polynomials
};

void polynomial::get_poly()      //  Get Polynomial
{
char c='y';
ptrn=ptrp=start=NULL;
while(c=='y' || c=='Y')
{
ptrn=new node;
if(ptrp!=NULL)
ptrp->next=ptrn;
if(start==NULL)
start=ptrn;
ptrp=ptrn;
cout<<"\nEnter the coefficient: ";
cin>>ptrn->coe;
cout<<"Enter the exponent: ";
cin>>ptrn->exp;
ptrn->next=NULL;
cout<<"Enter y to add more nodes: ";
cin>>c;
}
return;
}

void polynomial::show()  // Show Polynomial
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<coe<<"X^"<exp<<" + ";
ptr=ptr->next;
}
cout<<"\b\b ";
}
// Multiplying
void polynomial::multiply(polynomial p1,polynomial p2)
{
struct node *p1ptr,*p2ptr,*ptri,*ptrj;
int exp,coe;
ptrn=ptrp=start=NULL;
p1ptr=p1.start;
p2ptr=p2.start;
while(p1ptr!=NULL)   // Start multiplying
{
p2ptr=p2.start;
while(p2ptr!=NULL)
{
ptrn=new node;
ptrn->next=NULL;
if(start==NULL)
start=ptrn;
if(ptrp!=NULL)
ptrp->next=ptrn;
ptrn->coe=p1ptr->coe*p2ptr->coe;
ptrn->exp=p1ptr->exp+p2ptr->exp;
ptrp=ptrn;
p2ptr=p2ptr->next;
}
p1ptr=p1ptr->next;
}                    // end of multiplication
ptri=ptrj=start;     // Normalising
ptrn=ptrp=start=NULL;
while(ptri!=NULL)
{
exp=ptri->exp;
coe=ptri->coe;
ptrj=ptri->next;
while(ptrj!=NULL)
{
if(ptrj->exp==ptri->exp)
{
coe=coe+ptrj->coe;
ptrj->coe=0;//ptrj_p->next=ptrj->next;
}
ptrj=ptrj->next;
}
if(coe!=0)
{
ptrn=new node;
ptrn->next=NULL;
if(start==NULL)
start=ptrn;
if(ptrp!=NULL)
ptrp->next=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrp=ptrn;
}
ptri=ptri->next;
}
return;
}

void polynomial::sort()  // Sort Polynomials
{
struct node *ptri,*ptrj;
int coe,exp;
ptri=ptrj=start;
while(ptri->next!=NULL)
{
ptrj=ptri->next;
while(ptrj!=NULL)
{
if(ptri->expexp)
{
coe=ptri->coe;
exp=ptri->exp;
ptri->coe=ptrj->coe;
ptri->exp=ptrj->exp;
ptrj->coe=coe;
ptrj->exp=exp;
}
ptrj=ptrj->next;
}
ptri=ptri->next;
}
return;
}

int main()
{
clrscr();
polynomial p1,p2,product;
cout<<"First Polynomial.\n";
p1.get_poly();
cout<<"\nSecond polynomial.\n";
p2.get_poly();
clrscr();
cout<<"\nThe First polynomial is: ";
p1.show();
cout<<"\nThe second polynomial is: ";
p2.show();
cout<<"\n\nThe product of two polynomials is: \n";
product.multiply(p1,p2);
product.sort();
product.show();
getch();
return 0;
}

//---------------------- END--------------------
```

## Related Posts

#### Author: Arun Vishnu

• thanks 4 listing program …

• Hi everyone,

Can anyone send me the complete program of adding and multiplying two polynomials program using circular linked list in a very sample manner, it’s urgent…

• Thnx..i was using same logic but i don’t know why it was not executing…simply mistake may be there in my prgm….but its ok…

• thanks for programming help.

• sir
show some logic of dequeue.and b+ tree

• Thnks for help.

• Thank you for sharing this. I found it to be very useful and educational. Thank you!