Nov
28
2008
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->exp
exp)
{
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
Latest Posts
- How to Restrict Facebook friends using lists
- How to clean iPhone/iPad/iPod backup files
- Apple’s Siri On iPhone 4S vs Microsoft’s Tellme On Windows Phone
- Your Facebook account hacked? Here’s how to fix it
- Indian Railway Enabled VRM System to travel without the printout of e-ticket
- Amazon Launches $199 Kindle Fire Android Tablet
- Download Windows 8
- How to change APN settings in iPhone iOS Beta 7
- Steve Jobs Resigns as CEO of Apple
- How to open camera app when the iPhone is locked?
Tags
apple
c and cpp
Cheats
download
downloads
Entertainment
error
Film
friendship
Fun
gadgets
Games
gmail
google
greetings
india
Internet
iphone
java script
linux
love
mac
microsoft
mobile
News
office
onam
Personal
photos
Programming
review
Security
software
source code
technology
Tips & Tricks
video
videos
virus
web
web development
windows
windows 7
worm
yahoo
Recent Comments
- cit on C++ Program to implement a stack using linked list
- Samen on C++ Program to create a graph and use Depth First Search(DFS)
- Reena on I Got My GirlFriend
- Your Facebook account hacked? Here’s how to fix it | bugs of a debugger on Are you a Facebook user? Know and update your privacy settings
- DGTimes on Download Windows 8

An article by
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…
Thanks in advance….
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