Question

In: Computer Science

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.

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

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

Solutions

Expert Solution

ANSWER :-

GIVEN THAT :-

//poly.h

class Node{
private:
double coefficient;
int exponent;
Node *next;
Node *prev;

public:
Node();
Node(double, int);
double get_coefficient();
int get_exponent();
Node *get_next();
Node *get_previous();
void set_coefficient(double);
void set_exponent(int);
void set_next(Node*);
void set_previous(Node*);
};

class LinkedList{
private:
Node *head;
public:
LinkedList();
LinkedList(Node*);
Node* create_node();
void insert();
void print();
int operator=(const int& x );
// LinkedList operator+(const LinkedList& pol);
// LinkedList* operator*(const LinkedList& pol);
LinkedList operator!(void);
  
};

// poly.cpp
// ###################### NOT COMPLETE ########################
// # need to implement polynomial addtion, complementation, multiplication, valuation.
#include<iostream>
#include<math.h>
#include "poly.h"
using namespace std;

// node class
Node::Node(){}
Node::Node(double c, int e){
coefficient = c;
exponent = e;
next = NULL;
prev = NULL;
}
double Node::get_coefficient(){
return coefficient;
}
int Node::get_exponent(){
return exponent;
}
Node* Node::get_next(){
return next;
}
Node* Node::get_previous(){
return prev;
}

void Node::set_coefficient(double c){
coefficient = c;
}
void Node::set_exponent(int e){
exponent = e;
}
void Node::set_next(Node*n){
next = n;
}
void Node::set_previous(Node*p){
prev = p;
}

// LinkedList Class

LinkedList::LinkedList(){
this->head = NULL;
}
LinkedList::LinkedList(Node*head){
this->head = head;
}
Node* LinkedList::create_node(){
double c;
int e;
cout<<"Enter Coefficient? ";
cin>>c;
cout<<"Enter Exponent? ";
cin>>e;
return new Node(c, e);
}
void LinkedList::insert(){
// insert at tail
Node *node = create_node();
if(!head){
head = node;
return;
}
Node *temp = head;
while(temp->get_next()){
temp = temp->get_next();
}
node->set_previous(temp);
temp->set_next(node);
  
}
void LinkedList::print(){
Node *temp = head;

while(temp){
cout<<temp->get_coefficient();
if(temp->get_exponent()){
cout<<"x^"<<temp->get_exponent();
}
  
if(temp->get_next()){
cout<<" + ";
}
temp = temp->get_next();
}
  
cout<<endl;
}

int LinkedList::operator=(const int& x){
cout<<"\nPolynomial : ";
this->print();
Node *temp = head;
double result = 0;
while(temp){
result += temp->get_coefficient()*pow(x, temp->get_exponent());
temp = temp->get_next();
}
cout<<"at x = "<<x<<" is "<< result;
}

LinkedList LinkedList::operator!(void){
head->set_coefficient(head->get_coefficient()*-1);
return *this;
}

#include<iostream>
#include "poly.cpp"
using namespace std;

// main.cpp

int main(){
LinkedList p1, p2, p3;
p1.insert();
p1.insert();
p1.insert();
p1.print();

// p2.insert();
// p2.insert();
// p2.insert();
// p2.print();

// p3 = p1 + p2;
// p3.print();
// p3 = p1*p2;
// p3.print();
p1 = 1;
p3 = !p1;
cout<<"\np3=!p1\n";
p3.print();
p1.print();
// p3 = p1+p2;
// p3.print();
  

return 0;
}

poly:main.o poly.o
g++ main.o -o poly

main.o: main.cpp
g++ -c main.cpp

poly.o: poly.cpp
g++ -c poly.cpp

clean:
rm *.o
poly

# Note: save as makefile without extension.

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.
(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)...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
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...
Assume that you want to implement binary search with a linked list. What would be the...
Assume that you want to implement binary search with a linked list. What would be the performance of this algorithm? Compare and contrast this algorithm with the implementation of binary search on traditional sorted array and give a discussion. Your discussion and analysis must explain what the possibilities, issues and consequences of such design are, and then explain whether these issues would exist in the traditional array approach. Your answer can be around 1-2 paragraph of writing backed-up with algorithmic...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT