I need to implement a linked list function called:
bool LinkedPolynomial::Add(const double co_item, const int exp_item
It is suppose to:
1) Return false if there is a Node with the same exp_item and do nothing else.
2) Check to see if the linked list is already empty, and if it ISN'T I am to add that node to the list.
3) Check to see if the linked list is empty, and if it IS I am to add the Node the END of the list and return true.
I think I'm close to a correct solution. Here is my work for far:
bool LinkedPolynomial::Add(const double co_item, const int exp_item)
{
Node* count = head_ptr_;
while(count != NULL)
{
if (count->GetExponent() == exp_item)
{
return false;
}
count = count->GetNext();
}
Node* next_node_ptr = new Node(co_item, exp_item);
if (head_ptr_ == NULL) // For first addition to the list.
{
head_ptr_ = next_node_ptr; // New node is now first node
}
Node* count_2 = head_ptr_;
while (count_2 != NULL) // For any addition to the list that isn't the first addition.
{
if (count_2->GetNext() == NULL)
{
next_node_ptr = new Node();
}
count_2 = count_2->GetNext();
}
item_count_++;
return true;
}
Are my iterative loops correct? I feel this is a sloppy implementation. I would like to do the second and third steps at the same time, but can't think of a way.
Aucun commentaire:
Enregistrer un commentaire