---------------------------------- " Data Structure "---------------------------------------------
Addition & Multiplication of two Sparse Matrix Using Linked-List
Addition & Multiplication of two Sparse Matrix Using Linked-List
#include<iostream>
using namespace std;
//Structute of polynomial
typedef struct term
{
int coff;
int expo;
struct term *next;
}TERM;
class poly
{
TERM *head;
int NoOfTerms;
public:
poly()
{
head=NULL;
NoOfTerms=0;
}
//Create polynomial
void create_poly()
{
//int val;
char ch;
TERM *last;
//cout<<"How many nodes you want to enter"
do{
TERM *temp;
temp=new TERM;
cout<<"Enter coff: ";
cin>>temp->coff;
cout<<"Enter exponant:";
cin>>temp->expo;
temp->next=NULL;
if(head==NULL)
{
head=temp;
NoOfTerms++;
last=temp;
}
else
{
last->next=temp;
last=temp;
NoOfTerms++;
}
cout<<"Do want to enter Terms (y/n): ";
// flushall();
cin>>ch;
if(ch=='n')
break;
}while(1);
}
//Display function of polynomials
void print_poly()
{
TERM *trv;
trv=head;
while(trv!=NULL)
{
if(trv->expo!=0)
{
cout<<trv->coff<<"X"<<trv->expo;
trv=trv->next;
if(trv!=NULL&&(trv->coff>0))
cout<<"+";
}
else
{
cout<<trv->coff;
trv=trv->next;
}
}
// cout<<" ";
}
//Insertion of polynomials
void insert_term(TERM *t)
{
TERM *trv,*temp;
temp=new TERM;
temp->coff=t->coff;
temp->expo=t->expo;
temp->next=NULL;
if(head==NULL)
{
head=temp;
NoOfTerms++;
}
else
{
trv=head;
while(trv->next!=NULL)
{
if(trv->expo==temp->expo)
{
trv->coff=trv->coff+temp->coff;
NoOfTerms++;
return;
}
trv=trv->next;
}
if(trv->expo==temp->expo)
{
trv->coff=trv->coff+temp->coff;
NoOfTerms++;
return;
}
trv->next=temp;
NoOfTerms++;
}
}
//Addition of polynomials
poly add_poly(poly p)
{
TERM *t1,*t2,*temp;
t1=head;
t2=p.head;
poly res;
temp=new TERM;
while(t1!=NULL&&t2!=NULL)
{
if(t1->expo==t2->expo)
{
temp->coff=t1->coff+t2->coff;
temp->expo=t1->expo;
res.insert_term(temp);
t1=t1->next;
t2=t2->next;
}
else if(t1->expo>t2->expo)
{
temp->coff=t1->coff;
temp->expo=t1->expo;
res.insert_term(temp);
t1=t1->next;
}
else
{
temp->coff=t2->coff;
temp->expo=t2->expo;
res.insert_term(temp);
t2=t2->next;
}
}
while(t1!=NULL)
{
temp->coff=t1->coff;
temp->expo=t1->expo;
res.insert_term(temp);
t1=t1->next;
}
while(t2!=NULL)
{
temp->coff=t2->coff;
temp->expo=t2->expo;
res.insert_term(temp);
t2=t2->next;
}
return res;
}
//Multiplication of polynomials
poly mult_poly(poly p)
{
TERM *t1,*t2,*temp;
t1=head;
t2=p.head;
poly res;
temp=new TERM;
while(t1!=NULL)
{
t2=p.head;
while(t2!=NULL)
{
temp->coff=t1->coff*t2->coff;
temp->expo=t1->expo+t2->expo;
res.insert_term(temp);
t2=t2->next;
}
t1=t1->next;
}
return res;
}
};
//Menu of polynomials
void main()
{
poly P1,P2,P3;
cout<<"\nFirst polynomial\n: ";
P1.create_poly();
cout<<"\nSecond Polynomial\n: ";
P2.create_poly();
cout<<"\nFirst Polynomial is\n";
P1.print_poly();
cout<<"\n Second Polynomial is\n";
P2.print_poly();
cout<<"\n Addition of Polynomial is\n";
P3=P1.add_poly(P2);
P3.print_poly();
cout<<"\n Multiplication of Polynomial is\n";
P3=P1.mult_poly(P2);
P3.print_poly();
cin.get();
cin.get();
}
No comments:
Post a Comment