Deleting a whole linked list

16,521

Solution 1

While your delete function works correctly, it doesn't set the head in the caller to NULL when you do head = NULL; as you are only modifying a local pointer, which causes to your later logic where you are trying to print the values by checking the value of head.

To modify the original pointer, pass a pointer to head and set *head=NULL;

void destroy(set_elem **head)
{
set_elem *current = *head;

/* rest is the same */

*head = NULL;
}

Solution 2

when you copy head pointer passed into the function, head node is unaffected. You have to pass reference to the pointer to the head,then you will be able to delete head pointer. This can be also done by passing a pointer to the pointer to the head of the linked list but I find passing a reference more convenient. Following are the changes to your code.

void destroy(set_elem*& head)
{

  set_elem* current = head;
  set_elem* next;

  while (current != NULL)
  {
       next = current->next;
       free(current);
       current = next;
  }

   *head = NULL;
}

Solution 3

You are not changing the original head. You should pass the pointer to this head i.e. pointer to a pointer or should return changed head to the caller. Following is the code to return changed head to the caller. There are other answers showing pointer to pointer approach.

  set_elem* destroy(set_elem *head) {
      set_elem *current = head;
     set_elem *next;

     while (current != NULL) {
         next = current->next;
         free(current);
         current = next;
     }
     head = NULL;
     return head;
   }

in caller,

    head = destroy(head);
Share:
16,521

Related videos on Youtube

user90790
Author by

user90790

Updated on September 14, 2022

Comments

  • user90790
    user90790 over 1 year

    I am working with linked lists and i fill the structure but when I delete the whole structure and try to print the contents of the linked list (should be null), a list of numbers appear. I know it is probably a memory problem. Any suggestions of how to fix it?

    Code for deleting whole linked list:

    void destroy(set_elem *head)
    {
        set_elem *current = head;
        set_elem *next;
    
        while (current != NULL)
        {
           next = current->next;
           free(current);
           current = next;
        }
        head = NULL;
    }
    
    • Kerrek SB
      Kerrek SB over 10 years
      After you delete the whole structure, you're simply not allowed to print it anymore, that's all.
    • Drew Delano
      Drew Delano over 10 years
      I think the problem is that you're setting the local head pointer to NULL, but not the head pointer in the caller. You'd need a double pointer for that.
  • user90790
    user90790 over 10 years
    how can I alter it in this method? because I am lost!
  • P.P
    P.P over 10 years
    @user90790 Updated my answer with how to modify your function. I hope you really understand what I said before the code part.
  • user90790
    user90790 over 10 years
    yes I understand that but my prototype should be void destroy(set_elem* head); (GIVEN) and even when I used this solution, my file.exe stopped working
  • P.P
    P.P over 10 years
    @user90790 If you can't change the prototype, stick to your old function and set head to NULL immediately after calling: destroy(head); head=NULL; If you still have issues then your problem is elsewhere and post the relevant code or use a debugger.
  • user90790
    user90790 over 10 years
    it gives the this error: expected ';', ',' or ')' before '&' token|
  • pburka
    pburka over 10 years
    This is a C question. This answer is for a different language (probably C++).
  • java_doctor_101
    java_doctor_101 over 10 years
    Hi, This is a c++ implementation. Your code wasn't specific enough to determine the language. I forgot to look at your tags. If you use c++ this should work.