Let's assume I'm only allowed to use new and delete and no smart pointers in my implementation. I am not using sentinel nodes.
Below is my implementation for pop_back:
void sLinkedList::pop_back()
{
if(begin == nullptr)
{
std::cerr << "There is nothing to pop." << std::endl;
}
else
{
node* nodeToDelete = begin;
while(nodeToDelete->next != nullptr)
{
nodeToDelete = nodeToDelete->next;
}
delete nodeToDelete;
nodeToDelete = nullptr;
--mSize;
}
}
What it does is it creates a pointer to a node nodeToDelete and it iterates through the whole list until it reaches the last node (the node that points to nullptr). Then I delete that last node, set it equal to nullptr, and everything should be fine because the node before this (previously the second-to-last node) now points to a nullptr, marking the end of the list.
When I ran main with the following instructions:
int main()
{
sLinkedList newList;
std::cout << "Length is " << newList.length() << std::endl;
newList.print();
newList.push_front(4);
std::cout << "Length is " << newList.length() << std:: endl;
newList.print();
newList.pop_back();
std::cout << "Length is " << newList.length() << std::endl;
newList.print();
}
I get the output:
Length is 0
The list is empty.
Length is 1
4
Length is 0
4 // should print "There is nothing to pop."
Adding another pop_back directly after the first results in an Aborted (core dumped) signal.
Why doesn't the idea work?