Linked list question

This is a discussion on Linked list question within the C Programming forums, part of the General Programming Boards category; Hi, I am having difficulty deleting nodes from a singly linked list. If I have the following structure: NODE1----->NODE2----->NODE3 ...and ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    6

    Post Linked list question

    Hi,

    I am having difficulty deleting nodes from a singly linked list.

    If I have the following structure:

    NODE1----->NODE2----->NODE3

    ...and wish to delete NODE2, I have been setting a temp pointer to NODE3 (as NODE2->next), with a view to making NODE1 point to NODE3. The problem is, my NODE3 pointer is in fact NODE2->next, which is set to NULL as soon as I free up the memory for NODE2.

    How can I set a pointer to NODE3 without losing it when I get rid of NODE2 (without using pointers to the previous node)?

    Thanks for any suggestions.

    Paul

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Post your code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    tmp = nodetodelete->next;
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >without using pointers to the previous node
    You must have a pointer to the previous node at some point, whether it be built into the list or explicit in your algorithm. The idea is thus:
    Code:
    /* walk is node1, walk->next is the node to remove */
    save = walk->next;
    /*
      node1-->node2-->node3
              ^
              |
      save-----
    */
    walk->next = walk->next->next;
    /*
      node1------------>node3
                        ^
      save---->node2----|
    */
    free ( save );
    /*
      node1-->node3
    */
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    6

    Thumbs up

    Thanks very much, I'm pretty sure that answers my question. If not I'll post some code

    cheers

    Paul

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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