Doubly Linked List Polynomial Addition Multiplication
Doubly Linked List Polynomial Addition Multiplication
h>
#include<iostream>
class node
{
public:
int exp;
int coef;
node* next;
node* prev;
class cdll
{
public:
node *front;
node *rear;
cdll()
{
front = NULL;
rear = NULL;
void inspos(int,int);
void insert(node*,int,int);
void del(node*);
void display();
};
if(front == NULL)
{
cout<<"\nEmpty";
return;
}
if(front!=rear)
{
while(t!=rear)
{
cout<<" "<<t->coef<<" x^"<<t->exp<<" +";
t = t->next;
}
}
cout<<" "<<t->coef<<" x^"<<t->exp;
if(front==NULL)
{
front = rear =n;
n->next = n;
n->prev = n;
}
else
{
if( p==rear)
{
n->next = front;
n->prev = rear;
rear->next = n;
front->prev = n;
if(e < front->exp)
{
front = n;
}
else
{
rear = n;
}
}
else
{
n->prev = p;
n->next = p->next;
p->next->prev = n;
p->next = n;
}
}
rear->next = front;
front->prev = rear;
}
insert(rear, c, e);
return;
}
else
{
do
{
if(t->exp == e)
{
t->coef = t->coef+c;
return;
}
t=t->next;
}while(t!=front);
}
}
do
{
sum.inspos(p->coef, p->exp);
p = p->next;
}while(p!=a.front);
do
{
sum.inspos(q->coef, q->exp);
q = q->next;
}while(q!=b.front);
return sum;
}
cdll mult( cdll a, cdll b)
{
cdll prod;
int c,e;
node *p, *q;
p = a.front;
do
{
q = b.front;
do
{
c = p->coef * q->coef;
e = p->exp + q->exp;
prod.inspos(c,e);
q = q-> next;
}while(q!=b.front);
p = p->next;
}while(p!=a.front);
return prod;
}
int main()
{
cdll a, b, sum, prod;
int m,n; int c,e;
cout<<"\na :";
a.display();
cout<<"\nb :";
b.display();
sum = add(a,b);
cout<<"\nSum = ";
sum.display();
prod = mult(a,b);
cout<<"\nProduct = ";
prod.display();
return 0;