delete last node

This is a discussion on delete last node within the C Programming forums, part of the General Programming Boards category; Hey guys im trying to delete the last node in a doubly linked list so far i have traversed and ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    delete last node

    Hey guys im trying to delete the last node in a doubly linked list so far i have traversed and found the end using a while loop then making PREVIOUS which is now the last one to equal NULL. Then i am freeing the node that i have malloced would this work for a doubly linked list?

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

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Yes. Did you even think about the problem before asking here ? I mean this is probably your 5th thread about the same easy data structure and for every problem there is, you create a new topic and ask the same mindless questions instead of using your brain and coming up with the solution yourself.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yes. You don't have to worry about any pointers other than previous->next because you're deleting the last node and don't have to update any back pointers.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    I was only asking if this was correct i may not be a genious but i am trying and i only ask for advice on how to approach it

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Thanks Noir thats what i thought

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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