Writing a LinkedList destructor?

后端 未结 6 1881
我在风中等你
我在风中等你 2020-12-30 05:36

Is this a valid LinkedList destructor? I\'m still sort of confused by them.

I want to make sure I\'m understanding this correctly.

 LinkedList::~Link         


        
6条回答
  •  星月不相逢
    2020-12-30 06:14

    Your code might be correct, you should try running it with e.g. valgrind and see what it says. However, I would write it like this:

    for (ListNode *current = head, *next; current; current = next) {
        next = current->next;
        free(current);
    }
    

提交回复
热议问题