C Linked-list Destroy Function

Related searches

I am trying to learn C, and as many people, I have been a little stuck with pointers. Anyways, I made a recursive function that destroys my linked list, but as I have debugged, when I am back from the function the head of the list is not null as it should be, so I'm guessing it's some basic misunderstanding with pointers. Here's the function:

void destroy(struct node *n) {
   if (!n) return;
   n = NULL;
void deleteList(struct node** head_ref)
  struct node* current = *head_ref;
  struct node* next;
  while (current != NULL) {
    next = current->next;
    current = next;
  *head_ref = NULL;

Try like this ....you can change names as you want. If you still need help let me know.

Destroy a full linked list (c programming), You need to make curr point directly to head . A simpler version of the function might be e.g. this: void destroy(set_element **� this is kinda a continue of my last question about linked list. I have worked a little more on it and I got stuck at some functions I need to implement. The one I have question on right now is the destroy() function. It is supposed to release memory of every list_ item .

Head has been freed when this functions ends but it's not null. Everything in C is passed by value. So you pass a copy of the location of head into destroy. That memory is deallocated but head is not changed.

You could write this as:


void destroy(struct node** n){
   if(!*n) return;
   *n = NULL; 

Understanding a Linked List Destroy() Function, Hi, i'm now learning both pointers and structs (linked lists) lately, and have an example of a destroy function() for a given linked list. Hi, i'm now learning both pointers and structs (linked lists) lately, and have an example of a destroy function() for a given linked list. Would somebody mind explaining the processes within the function conceptually to me with as much detail as possible?

You have to use a pointer pointing to your list, calling with destroy(&n):

// clear complete list 
void destroy(struct node **n)
    if (*n == NULL)

    if ((*n)->next == NULL)
        *n= NULL;

    struct node *iter = *n;
    struct node *prev = NULL;

    // get last item and the previous one
    while (iter->next != NULL)
        prev = iter;
        iter = iter -> next;

    prev->next = NULL;


Hope this may help you.

Destroy function with Single linked list, Look at this part of the code: else { return destroy(head->next); free(head); return; }. The free(head); statement will never execute because the� Algorithm For C/C++: Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted. In Java, automatic garbage collection happens, so deleting a linked list is easy. We just need to change head to null.

Your recursive destroy function cannot modify the head variable in the caller's frame.

The statement n = NULL only affects the function argument, which is a local variable of the destroy function. It actually has no effect, so you can remove this statement.

You should set the head to NULL after the call to destroy in the caller function if it is needed there.

Destruction of Linked List Nodes: Destroy Item too?, Of course, this method of removal is much faster in a dually-linked list than a For generic data structures in C, I've often found it useful to require a type Function signature to destroy an element. typedef ListDestroy(void*� This video discusses the destroy helper function of IntLinkedList class, that de-allocates memory for IntLinkedList class that was allocated by the constructor and insert function. destroy

Here is the sample function to destroy Linked list using DeleteRear():

void Destroy_Using_Rear(List *L)
    int y;
    Node *P,*Q,*Z;

        return y;

std::list is a container that supports constant time insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is usually implemented as a doubly-linked list.

Clone a linked list with next and random pointer in O(1) space; Length of longest palindrome list in a linked list using O(1) extra space; Program for n’th node from the end of a Linked List; Find the middle of a given linked list in C and Java; Write a function that counts the number of times a given int occurs in a Linked List

A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the linked list is empty, then the value of the head is NULL. Each node in a list consists of at least two parts: 1) data 2) Pointer (Or Reference) to the next node In C, we can represent a node using structures.

Of course, this function definition will go in queue.c since it is part of the implementation. However, this function is different than the others because it is only a helper function for completing the queue, and is not meant to be called by people using a queue. Thus, you'll note a few differences: The function is declared to be a static

  • n is a local variable, so n = NULL only has local effect -- which means it has no effect at all, since it's at the end of the function.
  • Please show some code which demonstrates that the head is not null.
  • Expanding on what @ruakh said, if you want to modify a pointer, you must pass a pointer to that pointer (i.e. a struct node**), just like how you would have to pass a pointer if you wanted to modify anything else.
  • I thought that using pointers it wouldn't have the effect of a local variable... Guess I still don't know how to implement...
  • Do not use recursion. There are better ways using a loop and you will get better performance.
  • +1 for not using recurions (and thus avoiding the stackoverflow that is the inevitable consequence if the size gets large enough).
  • @Mats: he plagiarized it from breakinterview.com/write-a-function-to-delete-a-linked-list Additional evidence, he didn't even attempt to explain the problem and the solution in own technical terms. This answer is clearly merely a code dump he luckily found on Google along with a doubting "try this ...".
  • @BalusC head_ref is a starting of the list ...then next pointer points to the next of current ,afterwords space is freed pointed by current and current points to next and next points to next address of that current. while keeps on checking if it is not null...;) I did not get lucky, I know what I wrote man...else I wouldn't have written it,FYI it is not using recursion which is obvious that's y liked a comment of Ed Heal
  • Would this code be any different if struct node* next; was changed into struct node* next = current->next; and replaced the first line of the while loop?
  • Your call to clear() is unclear; did you mean to call destroy() again? Your loop to find the end before releasing something, then calling the function again recursively (with simple tail recursion at that) seems a little odd;. I think it will work (I've not tested it), but the original code with recursion and no loop is simpler — though it might recurse more deeply than your code does.
  • Oh, my bad. Indeed it should be destroy(). And yes, my function is a bit too circuitous and I will take a look at the other solutions posted here to improve my one. However, it works ;)