0% found this document useful (0 votes)
171 views

Doubly Linked List Polynomial Addition Multiplication

This C++ program defines classes and functions to represent and perform operations on polynomials represented as circular doubly linked lists (CDLL). It includes functions to insert nodes into the CDLL, display the polynomials, add two polynomials by inserting their terms, and multiply two polynomials by multiplying corresponding terms and adding the results. The main function tests the code by taking in two polynomials as input, displaying them, and computing and displaying their sum and product.

Uploaded by

Akshara P S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

Doubly Linked List Polynomial Addition Multiplication

This C++ program defines classes and functions to represent and perform operations on polynomials represented as circular doubly linked lists (CDLL). It includes functions to insert nodes into the CDLL, display the polynomials, add two polynomials by inserting their terms, and multiply two polynomials by multiplying corresponding terms and adding the results. The main function tests the code by taking in two polynomials as input, displaying them, and computing and displaying their sum and product.

Uploaded by

Akshara P S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<bits/stdc++.

h>

#include<iostream>

using namespace std;

class node
{
public:
int exp;
int coef;
node* next;
node* prev;

node(int coef, int exp)


{
this->coef = coef;
this->exp = exp;
this->next = NULL;
this->prev = NULL;
}
};

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();

};

void cdll:: display()


{
node *t = front;

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;

void cdll:: insert(node *p, int c, int e)


{

node* n = new node(c,e);

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;
}

void cdll::inspos(int c, int e)


{
node *t = front; node* n;
if(front == NULL)
{
insert(front,c,e);
}

else if(e < front->exp)


{
insert(rear,c,e);
return;
}
else if( e > rear->exp)
{

insert(rear, c, e);
return;
}
else
{
do
{
if(t->exp == e)
{

t->coef = t->coef+c;

return;
}

else if(t->exp < e && t->next->exp > e)


{
insert(t,c,e);
return;
}

t=t->next;
}while(t!=front);
}
}

cdll add (cdll a, cdll b)


{
cdll sum;
int val;
node *p, *q,*r;
p = a.front;
q = b.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<<"\nEnter no:of terms for first polynomial :";


cin>>m;
cout<<"\nEnter first polynomial (coef, exp):\n";
for(int i=0;i<m;i++)
{
cin>>c>>e;
a.inspos(c,e);
}

cout<<"\nEnter no:of terms for second polynomial :";


cin>>n;
cout<<"\nEnter second polynomial (coef, exp):\n";
for(int i=0; i<n;i++)
{
cin>>c>>e;
b.inspos(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;

You might also like