Question

In: Computer Science

Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...

Implementing Polynomials using Singly Linked List in C++

  1. The Term Class

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g.

    • in the term 4X2, the coefficient is 4 and the exponent 2
    • in -6X8, the coefficient is -6 and the exponent 8

    Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent


    II. The Polynomial Class

    Now create a class to represent a polynomial. As defined here, a polynomial is a sequence of terms. E.g.

    1. 3X2 + 4X4 + X6
    2. 2 + 5X2 + 6X3 + 2X7
    3. 4X10

    The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10)

    F To receive credit for this assignment, your class must use a generic “List of Term” to store the terms of a Polynomial object

    Your class will have a constructor that creates an empty list and additional methods to do each of the following:

    1. insert a new term in its proper place in a polynomial (see “Additional Specifications,” below)

    2. return all the terms of a polynomial as a single line string, as shown here:

    3x^2 + 4x^4 + x^6

    3. delete a term from a polynomial (see “Additional Specifications,” below)

    4. Compute and return the derivative of all the polynomial


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

Solutions

Expert Solution

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


Related Solutions

Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT