Destructor for a linked List

The problem lies here:

delete first;
first = first->next;

When you delete first, but then try to access first->next. Cache first->next into a temp variable of type node*, then do delete first to fix this:

struct node* temp;
while (first != NULL)
{
    temp = first->next;
    delete first;
    first = temp;
}

Leave a Comment