In: Computer Science
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head) ● Constructor ● Public Function to create a Node ● Public function to insert the Node to the linked list (sorted polynomial according to the exponent). ● Public function to print the polynomial in the elegant format: 23x 9 + 18x 7+3 ● Overloaded public function to allow adding two polynomials poly3=poly1+poly2 (23x 9 + 9x 7+3)+(2x 4+3x 7+8x 2 -6) =23x 9 +12 x 7+2x 4+8x 2 -3 ● Overloaded public function to allow negating (!) the sign of any polynomial poly3=!poly1 2x 4+3x 7+8x 2 -6 =- 2x 4 -3x 7+8x 2+6 ● Overloaded public function to allow multiplying two polynomials ● Public function to evaluate polynomial based on an input If x=1, then the value of this polynomial 2x 4+3x 7+8x 2 -6 should be 2(1) 4+3(1) 7+8(1) 2 -6 =7 3. Main ● Main menu to test the following tasks ○ cout << "1. Create polynomial \n"; ○ cout << "2. Print polynomial \n"; ○ cout << "3. Add two polynomilas \n"; ○ cout << "4. Negate polynomial \n"; ○ cout << "5. Multiply two polynomials \n "; ○ cout << "6. Evaluate polynomial \n "; ○ cout << "7. Exit \n";(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)(using single linkedlist only)
BELOW CODE HAVE ALL the functions that you need to solve this question-->
#include<bits/stdc++.h>
using namespace std;
class Node
{
double cof;
int pow;
Node *next;
public:
void set_cof(double c)
{
cof = c;
}
void set_pow(int x)
{
pow = x;
}
double get_cof()
{
return cof;
}
int get_pow()
{
return pow;
}
Node* get_next()
{
return next;
}
void set_next(Node *temp)
{
next = temp;
}
};
class Polynomial_Linked_List
{
Node *head;
public:
Polynomial_Linked_List()
{
head = new Node;
head = NULL;
}
Node* get_head()
{
return head;
}
Node* create_node( double x , int y , Node *temp)
{
temp = new Node;
temp->set_cof(x);
temp->set_pow(y);
temp->set_next(NULL);
return temp;
}
void insert_node(double x , int y)
{
Node *n ;
n = create_node( x , y , n );
if(head == NULL)
{
head = n;
}
else
{
Node *ptr = head;
while (ptr->get_next() != NULL)
{
ptr = ptr->get_next();
}
ptr->set_next(n);
}
}
void print_poly()
{
Node *ptr = head;
while(ptr->get_next() != NULL)
{
cout << ptr->get_cof() << "x^" << ptr->get_pow() << " + ";
ptr = ptr->get_next();
}
cout << ptr->get_cof() << "x^" << ptr->get_pow();
}
void add_poly( Polynomial_Linked_List one , Polynomial_Linked_List two )
{
Node *poly1 = one.get_head();
Node *poly2 = two.get_head();
while(poly1->get_next() && poly2->get_next())
{
if(poly1->get_pow() > poly2->get_pow())
{
insert_node( poly1->get_cof() , poly1->get_pow() );
poly1 = poly1->get_next();
}
else if(poly1->get_pow() < poly2->get_pow())
{
insert_node( poly2->get_cof() , poly2->get_pow() );
poly2 = poly2->get_next();
}
else
{
insert_node( poly1->get_cof()+poly2->get_cof() , poly1->get_pow() );
poly1 = poly1->get_next();
poly2 = poly2->get_next();
}
}
if(poly1->get_pow() == poly2->get_pow())
{
insert_node( poly1->get_cof()+poly2->get_cof() , poly1->get_pow() );
}
}
void removeDuplicates()
{
Node *ptr1, *ptr2, *dup;
ptr1 = get_head();
while (ptr1 && ptr1->get_next() != NULL)
{
ptr2 = ptr1;
while (ptr2->get_next() != NULL)
{
if (ptr1->get_pow() == ptr2->get_next()->get_pow())
{
ptr1->set_cof(ptr1->get_cof() + ptr2->get_next()->get_cof());
dup = ptr2->get_next();
ptr2->set_next(ptr2->get_next()->get_next());
delete (dup);
}
else
{
ptr2 = ptr2->get_next();
}
}
ptr1 = ptr1->get_next();
}
}
void mul_poly( Polynomial_Linked_List one , Polynomial_Linked_List two )
{
Node *poly1 = one.get_head();
Node *poly2 = two.get_head();
while(poly1)
{
while(poly2)
{
double coeff;
int power;
coeff = poly1->get_cof() * poly2->get_cof();
power = poly1->get_pow() + poly2->get_pow();
insert_node( coeff, power );
poly2 = poly2->get_next();
}
poly2 = two.get_head();
poly1 = poly1->get_next();
}
removeDuplicates();
}
void negate_polynomial()
{
Node *poly = get_head();
while(poly)
{
double coeff;
coeff = poly->get_cof();
coeff = coeff * (-1);
poly->set_cof(coeff);
poly = poly->get_next();
}
}
double evaluate_polynomial(double x)
{
Node *poly = get_head();
double result = 0 , coeff;
int power;
while(poly)
{
coeff = poly->get_cof();
power = poly->get_pow();
result += coeff * pow( x, power ) ;
poly = poly->get_next();
}
return result;
}
};
int main()
{
Polynomial_Linked_List poly1,poly2,poly;
poly1.insert_node(5,4);
poly1.insert_node(4,3);
poly1.insert_node(58,2);
poly1.insert_node(54,1);
poly1.insert_node(5,0);
poly1.print_poly();
cout<<"\n";
poly1.negate_polynomial();
poly1.print_poly();
cout<<"\n";
poly2.insert_node(5,4);
poly2.insert_node(4,3);
poly2.insert_node(58,2);
poly2.insert_node(54,1);
poly2.insert_node(5,0);
poly2.print_poly();
cout<<"\n";
poly.mul_poly( poly1 , poly2 );
poly.print_poly();
cout<<"\n"<<poly2.evaluate_polynomial(5);
}
Output->