For a class project in my Object Oriented Programming class, we have been tasked with writing a program that creates a Polynomial Data Type. The Polynomial is to be a singly linked list of Nodes that contain a term.
A term and a node containing said term would be like the following:
struct Term {
double coefficient;
unsigned int exponent;
};
struct Node {
Term term;
Node* next;
};
I have a class called Polynomial
that has some methods for creating and manipulating the polynomial:
class Polynomial {
public:
Polynomial();
~Polynomial();
void addTerm(Term term);
unsigned int degree();
double coefficientFor(unsigned int exponent);
void clear();
private:
Node* Head;
};
We are to construct the Polynomial to have an initial term that has a value of 0:
Polynomial::Polynomial()
{
Term term_construct;
term_construct.coefficient = 0;
term_construct.exponent = 0;
Head->term = term_construct;
Head->next = NULL;
}
My problem is no arising with the addTerm
method. The method takes a term and then inserts a node with that term into the singly linked list. But there is a catch. The linked list must be ordered in descending order based on the exponent of the term that is passed into the method.
This is all I have so far:
void Polynomial::addTerm(Term term)
{
Node* Temp = new Node;
Temp->term = term;
Temp->next = NULL;
}
For example, if terms with exponents 6, 7, 3, and 2 were entered, then the head node would contain the term with exponent = 7, the next node would have exponent = 6, and so on.
Also, if a term is entered that has an exponent that is equivalent to an exponent of another existing term, instead of adding the term to the list, the coefficient of the two terms must be added together. If the coefficient is equal to 0 after adding the two terms together, the node must be deleted from the linked list.
I have always struggled with understanding linked lists and how to implement them. But with the additional requirements for adding the Nodes to the linked list, I have become completely lost in what I am doing.
Can anyone help lead me down the right path?