Question

In: Computer Science

(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...

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

Solutions

Expert Solution

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


Related Solutions

In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT