In: Computer Science
Implementing Polynomials using Singly Linked List in C++
5. reverse the order of the terms in a polynomial (see “Additional
Specifications,” below)
III. The Test Class
The main method of your test class will create a Polynomial object
and then read and process a series of operations
The operations are:
1. INSERT X Y
Insert a new term with coefficient X and exponent Y into its proper
place in the polynomial
(insert method : adds terms to the list in descending order of
power)
2. DELETE X Y
Remove the term with coefficient X and exponent Y from the
polynomial
3. REVERSE
Reverse the order of the terms of the polynomial
4. 1st DEV
to find the first derivatives of the polynomial
2nd DEV
to find the
second derivatives of the polynomial
Each operation is to be carried out by calling a method of the
Polynomial class
Each operation read must be “echo printed” to the screen
After each operation, print the updated polynomial by calling the
toString() method
For the Derivatives operation, print the string returned
-----------------------------------------------------------------------------
#include<iostream>
#include<string>
using namespace std;
class Term
{
private :
int coeff;
int expo;
public :
Term *next;
Term()
{
next=NULL;
}
Term(int c,int e)
{
coeff=c;
expo=e;
next=NULL;
}
int getCoeff(void)
{
return
coeff;
}
int getExpo(void)
{
return
expo;
}
};
class Polynomial
{
public :
Term *head;
Polynomial()
{
head=NULL;
}
void insert(int x,int y)
{
Term *temp=head;
Term *pre=NULL;
while(temp!=NULL&&temp->getExpo()>y)
{
pre=temp;
temp=temp->next;
}
Term *node=new Term(x,y);
if(pre==NULL)
{
head=node;
}
else
{
pre->next=node;
node->next=temp;
}
}
string printPolynomial(Term *Head)
{
Term *temp=Head;
int count=0;
string str("");
if(temp==NULL)
return str;
while(temp->next!=NULL)
{
int
coeff=temp->getCoeff();
int
expo=temp->getExpo();
if(coeff<0)
coeff=coeff*-1;
string s("");
s.append(to_string(coeff)).append("x^").append(to_string(expo));
if(coeff<0)
{
str.append(" - ").append(s).append(" ");
}
else
if(count>0)
{
str.append(" + ").append(s).append(" ");
}
else
{
str.append(s).append(" ");
}
count++;
temp=temp->next;
}
int
coeff=temp->getCoeff();
int
expo=temp->getExpo();
str.append(to_string(coeff)).append("x^").append(to_string(expo));
return str;
}
void reverse()
{
Term* current = head;
Term *prev = NULL, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
Term* derivation(Term *Head)
{
Term *temp=Head;
Term *p=NULL,*pre=NULL;
while(temp!=NULL)
{
if(temp->getExpo()>1)
{
Term *node=new
Term(temp->getExpo()*temp->getCoeff(),temp->getExpo()-1);
if(p==NULL)
{
p=node;
pre=node;
}
else
{
pre->next=node;
pre=node;
}
}
temp=temp->next;
}
return p;
}
void deleteTerm(int x,int y)
{
Term *pre=NULL,*temp=head;
if(head!=NULL&&head->getCoeff()==x&&head->getExpo()==y)
{
head=head->next;
delete(temp);
return ;
}
while(temp!=NULL&&temp->getCoeff()!=x&&temp->getExpo()!=y)
{
pre=temp;
temp=temp->next;
}
if(temp==NULL)
return;
pre->next=temp->next;
delete(temp);
}
};
int main()
{
Polynomial p1;
p1.insert(4,5);
p1.insert(-3,2);
p1.insert(2,4);
string str=p1.printPolynomial(p1.head);
cout<<str<<endl;
p1.reverse();
str=p1.printPolynomial(p1.head);
cout<<str<<endl;
Term *node=p1.derivation(p1.head);
cout<<p1.printPolynomial(node)<<endl;
p1.deleteTerm(2,4);
str=p1.printPolynomial(p1.head);
cout<<str<<endl;
}