Removing First Item From Single Link List

My algorithm is not working
1
2
3
4
5
6
7
8
9
10
11
	void remove_first()
	{
		current_node = head_node;
		node * temp_node = current_node;
		current_node = current_node->get_next();
		head_node->set_next(current_node);
		delete temp_node;
		temp_node = NULL;
	}

Define "not".
1
2
3
4
5
6
7
8
9
void remove_first()
{
	current_node = head_node;
	node * temp_node = current_node;
	current_node = current_node->get_next();
	head_node->set_next(current_node);
	delete temp_node;
	temp_node = NULL;
}


current_node == head_node aka first node (non-local pointer?)
temp_node == first node
current_node == second node
first node points to second node, previously it was pointing to second node
remove first node aka head_node (if class node has destructor, it can cascade)
head_node remains invalid
current_node has reference to former second node
Last edited on
Well, I think that head_node is first. Then you need to do it differently:
1
2
3
4
5
6
7
8
9
	void remove_first()
	{
		if(head_node)
		{
			node * temp_node = head_node;
			head_node = head_node->get_next();
			delete temp_node;
		}
	}
Topic archived. No new replies allowed.