Delete all from linked list

This is a discussion on Delete all from linked list within the C Programming forums, part of the General Programming Boards category; Hi ,I have trable with deletiing all records in linked list Code: void dell(record **head) { while(*head!=NULL) { * head=(*head)->next; ...

  1. #1
    lio
    lio is offline
    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,175
    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
    lio
    lio is offline
    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,175
    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, 09:04 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21