Thread: Free a link list

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Free a link list

    To free the memory of a link list so I have to travel though each node and free it?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by EvilBaby
    To free the memory of a link list so I have to travel though each node and free it?
    Of course you do. For every malloc call, you need a free call.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Quote Originally Posted by quzah
    Of course you do. For every malloc call, you need a free call.

    Quzah.

    damn, my link list is 45 000 + links, this is gonna take a while

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    just out of curiosity should this work


    Code:
    while (current != NULL)
    {
    	if (current->next != NULL)
    	{
    		temp = current->next;
    		free (current);
    		current = temp;
    	}
    	else
    		free(current);
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Normally you'd do something like this
    Code:
    while (current)
    {
      temp = current->next;
      free(current);
      current = temp;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >damn, my link list is 45 000 + links, this is gonna take a while
    45,000+ really isn't that much for any realistic application.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    thanks for the help

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Quote Originally Posted by Prelude
    >damn, my link list is 45 000 + links, this is gonna take a while
    45,000+ really isn't that much for any realistic application.

    it is when you were tyring to do it the way I originally thought I had to. See I'm new at this so I started off by going to the last element, free, that, then go through the list to the last element again and free that and do that over 45 000 times.


    However I soon realized there was a much better way of doing it and as it turns out I was almost right.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yea if you are trying to free the last element everytime it'll take a while. The "standard" way is to free the head.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yea if you are trying to free the last element everytime it'll take a while
    Only if you don't have a tail pointer and a double linked list or a suitable single linked setup for tail deletion.

    >I started off by going to the last element, free
    Yikes. Yes, that would be rather inefficient for a list of that size.
    My best code is written with the delete key.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    hmm have to remember the tail pointer idea.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And just because...
    Code:
    void nocircularlistsplease(struct node*n)
    {
        if(n&&n->next)
            nocircularlistsplease(n->next);
        free(n);
    }
    But on a very big list you may run out of stack space.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM