Have a final project on Linked List. I know that there are some errors in the push_back and pop_back since because of them .cpp driver does not work, but I don't know what is wrong.
Commented pop_back function looks more efficient, but it also has some error.
Help to fix, please) As well, maybe somebody know how to reference to CarPart class through List class in printList (driver.cpp)? What is specified in the conditions for loop? Thank you!
#include "List.h"
// A push_back function that takes a Node* as a parameter and adds the node pointed
// to by the pointer to the end of the list.
void List::push_back(Node* n)
{
if (first == nullptr)
{
first = n;
last = first;
// head = new Node<T>(n);
}
else
{
Node *cursor = first;
while (cursor->getNext() != NULL)
{
cursor = cursor->getNext();
}
cursor = NULL;
}
}
// A push_front function that takes a Node* as a parameter and adds the node pointed to by the pointer to the front of the list.
void List::push_front(Node* n)
{
if (first == nullptr)
{
first = n;
last = first;
number++;
}
else
{
n->setNext(first);
first = n;
number++;
}
}
// A pop_back function that removes the last node from the list,
// and returns a pointer to this node.Important, do not delete the node in this function.
// Simply unlink it from the list.
//Node* List::pop_back()
//{
// Node* temp = first;
// if (first == nullptr)
// {
// return nullptr;
// }
//
// else if (first == last)
// {
// first = nullptr;
// last = nullptr;
//
// }
//
// else
// {
// while (!(temp.getNext = last))
// {
// temp = temp.getNext();
// }
// last.setNext(nullptr);
// temp = temp.getNext();
// return temp;
// }
//}
Node* List::pop_back()
{
Node *cursor = first;
if (first == nullptr)
{
returnnullptr;
}
while (cursor->getNext())
{
cursor = cursor->getNext();
}
delete cursor->getNext();
returnnullptr;
}
// A pop_front function that removes the first node from the list, and returns a pointer to this node.
// Important, do not delete the node in this function.Simply unlink it from the list.
Node* List::pop_front()
{
if (first == nullptr)
returnnullptr;
else
{
Node* temp = first;
first = first->getNext();
number--;
return temp;
}