I do not know how you would write the function. As for me I would write it the following way.
void pop_front()
{
if ( head )
{
node *tmp = head;
head = head->link;
delete tmp
}
}
Question
As an exercise, I'm creating my own container class template called List. It's a single-end linked list based off a template, and the node is a struct that is nested in the list itself, like so:
template<typename T>
class list
{
protected:
struct node
{
T data;
node* link;
};
node* head;
//Rest of the code after here
};
The container has two functions to build the list: push_front and pop_front. I managed to figure out the push_front function, however I have trouble figuring out how to write a pop_front function. The pop_front function removes the head of the list and sets the next value in the list as the head. I don't know how an actual pop_front function works, so I'm stuck here. What should I do next?
Solution
I do not know how you would write the function. As for me I would write it the following way.
void pop_front()
{
if ( head )
{
node *tmp = head;
head = head->link;
delete tmp
}
}