Thread: Delete all from linked list

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Delete all from linked list

    Hi ,I have trable with deletiing all records in linked list
    Code:
    void dell(record **head)
    {
       while(*head!=NULL)
      {
        * head=(*head)->next;
       }
       free(*head);
    }

    Thank you for the help

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You need to utilize a temporary pointer to point to next element before freeing it:
    Code:
    record *rnext;
    while(*head)
    {
      rnext = (*head)->next;
      free(*head);
      *head = rnext;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks a lot

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void nom( struct node *n )
    {
        if( n )
        {
            nom( n->next );
            free( n );
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sometimes recursion really is more elegant.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM