# Linked Lists

Orçamento $30-5000 USD

Overview

For this assignment, you will be implementing a singly linked list to store polynomials. For instance, the polynomial 5*X^4 + 3*X^3 + 7*X + 10 will be stored as a linked list made up of 4 nodes. The first node will contain the coefficient of 5 and exponent of 4. It will be followed by a node with coefficient and exponent of 3, a node with coefficient of 7 and exponent 1, and finally a node with coefficient 10 and exponent 0. Notice that the nodes are stored in descending order based on the exponent.

class Term

This class will store information about one term in the polynomial, therefore this is the class that is used as the nodes in the linked list.

The definition for this class should be contained in a file called ***Term.h***, while the implementation should be contained in a file called ***[url removed, login to view]***. The Poly class (described below) is eventually going to need access to the private members of the Term class.

Data members

This class should have three data members that are not available to the outside world. The first is an integer that is used to hold the coefficient for a term in the polynomial. The second is an integer that is used to hold the exponent for a term in the polynomial. The third is pointer to a Term object, which will hold the address of the next Term in the linked list.

Constructor

This class should have one constructor. It takes three arguments: the first is an integer value that will be used to set the coefficient, the second is an integer that will be used to set the exponent, and the third is a pointer to a Term object that will be used to set the link field for the Term.

All three of the arguments should have default values of 0.

### Methods

Input (or extraction) operator: istream >> Term

Overload the input operator to read in two integers, a coefficient and an exponent, from one line. The two integers should be stored in the appropriate fields of the Term.

#### Output (or insertion) operator: ostream << Term

Overload the output operator to print a Term in the following format:

coefficient*X^exponent

There are a couple of special cases that need to be handled:

1. If the exponent is 1, print only coefficient*X

2. If the exponent is 0, print only the coefficient.

## Deliverables

continued.....

Multiplication Operator: Term * Term

Overload the multiplication operator to multiply two Term objects. This is a constant method that takes one argument, a reference to a constant Term, and returns a Term object.

The new Term that is created should have an exponent equal to the sum of the exponents for the two Terms, a coefficient equal to the product of the coefficients for the two Terms, and a pointer field that is 0. For instance:

class Poly

This class will store the polynomial, therefore this is the class that is used as the linked list.

The definition for this class should be contained in a file called Poly.h, while the implementation should be contained in a file called Poly.cc. The Poly class is going to use the Term class so Term.h should be one of the #included files.

For this class, you should have at least the following data member and methods:

Data member

This class should have one data member that is not available to the outside world. It is a pointer to a Term object, which will hold the address of the first Term in the linked list.

Constructors

Default Constructor

The default constructor should initialize the pointer to 0 to indicate that the linked list is empty.

Copy Constructor

The copy constructor for this class should create a new Poly object that has Term's that are identical to those in an existing Poly object. It takes as its argument a reference to a constant Poly object.

Think about calling the Copy() method described below.

Destructor

The destructor for this class should release all of the Terms in a Poly object. Think about calling the Dismantle() method described below.

Methods

Copy

This is a private helper method that creates a copy of an existing Poly object. It takes two arguments and returns nothing. The first argument is a pointer to a Term object, while the second is a reference to a pointer to a Term object (ie. Term *&).

This method is going to create a copy of the existing Poly object one Term at a time. It can be implemented recursively.

The initial call to Copy will have sourcePtr pointing at the first Term of an existing Poly object, and destinationPtr referencing the first Term of the copy of the Poly object that is being created.

Dismantle

This is a private helper method that deletes all of the Terms in an existing Poly object. It takes one argument and returns nothing. The argument to this method is a reference to a pointer to a Term object.

This method will delete the Terms one at a time. It can be implemented recursively.

Equality Operator: Poly == Poly

Overload the equality operator to determine if two Poly objects are equal. Two polynomials are equal if the corresponding coefficients and exponents of every Term are equal.

Output (or insertion) Operator: ostream << Poly

Overload the output operator for the purpose of printing out an instance of the Poly class. The function should print out the polynomial one Term at a time.

This can be implemented by starting a pointer at the first Term in the Poly object and stepping through the Terms as long as the pointer is not 0. At each step, the Term is printed using the output operator for Term.

If the Poly object is empty (i.e. there are no Terms to print), the function should print a message saying so.

This function is going to need access to private data from BOTH the Poly and Term classes. Therefore, this function should be made a friend of BOTH the Poly and Term class.

Input (or extraction) Operator: istream >> Poly

Overload the input operator to read in the data to form a Poly object. The function is first going to read in an integer that is equal to the number of Terms that will make up the polynomial (lets call this numTerm). Now implement a loop that will execute numTerm times. Inside of the loop:

## Platform

Tera Term Pro 2.3

continued...

Add. Operator: Poly += Term

Overload += operator to add Term to an existing Poly object. This takes 1 argument, a reference to a constant Term, and returns reference to a Poly object.

Assignment Operator: Poly = Poly

Overload assign. operator so that it assigns contents of the Poly on the right of the operator to the Poly on left. Takes 1 argument, reference to a constant Poly, and returns reference to a Poly.

Add. Operator: Poly + Poly

Overload add. operator so that two instances of Poly can be added together. Takes 1 argument, reference to a constant

Poly object, and returns a Poly object.

Sub. Operator: Poly - Poly

Overload subtraction operator so one instance of Poly can be subtracted from another. Takes 1 argument, reference to a constant Poly object, and returns Poly object.