Deleting Linked List

Anyone know why the first code doesn't work while the second one does?

1
2
3
4
5
6
7
8
Node* cur = first;
Node* tmp;
while (cur != 0)
{
	tmp = cur;
	cur = cur->next;
	delete tmp;
}


1
2
3
4
5
6
7
8
Node** cur = &first;
Node* tmp;
while (*cur != 0)
{
	tmp = *cur;
	*cur = tmp->next;
	delete tmp;
}
Last edited on
The first code would work if we add first = nullptr ; after the loop.

1
2
3
4
5
6
7
8
9
node* cur = first ;
while( cur != nullptr ) // favour nullptr over zero
{
    node* tmp = cur ;
    cur = cur->next ;
    delete tmp ;
}

first = nullptr ;
Thanks :)
Topic archived. No new replies allowed.