Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I have the address of a particular node in single linked list, i have to delete that node only??? how is this possible???...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    Delete a node???

    Hi All,

    I have the address of a particular node in single linked list, i have to delete that node only??? how is this possible???

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by dayananda.ms View Post
    Hi All,

    I have the address of a particular node in single linked list, i have to delete that node only??? how is this possible???
    That depends on your code.

    To delete a node usually the concrete operations depends on the position of the node (in circular lists is would be all the same). Simple list usually have an initial node, a final one, and some intermediate nodes.

    You need a temp node. To delete the initial node, you would point the temp node to the initial node, then the new initial node to the second node, then free the temp one.

    To delete the final node you point the temp node to it, then the new final node to the previous one, adjust it's next field to null or whatever and then free temp.

    To delete an intermediate node, you need to point temp to that node, then modify the relevant fields on the prev and next nodes, so they point to each other, and then free temp.

    Very generalist, but there are thousands of examples of implementations of linked lists all around the net. It's one of the most basic data structures around and there's really no point in replicating the code once again.

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Why do you need a temp node?

    To delete the initial node, you just set the start pointer to the pointer in the deleted node.

    To delete an intermediate node you set the previous node's pointer to the pointer from the deleted node.

    The last node in a single linked list is treated the same as an intermediate.

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by scm View Post
    Why do you need a temp node?

    To delete the initial node, you just set the start pointer to the pointer in the deleted node.

    To delete an intermediate node you set the previous node's pointer to the pointer from the deleted node.

    The last node in a single linked list is treated the same as an intermediate.
    It depends on the language you use.

    On C that's the best way to leak memory since it has not a garbage collector like Java. So you need to point a temp node so you can free it after you have adjusted the other nodes conveniently. Java has a garbage collector so that shouldn't be a problem (though I have virtually zero experience with java on the real world, and it's been ages since I used it on the faculty).

    Of course, this is all assuming that you reserved the memory with malloc() or a similar function.

    EDIT: Yes, you could as well free() it before fixing the other pointers, but I am generally reluctant to leave the lists on an inconsistent state, since those pointers would be pointing to free arbitrary memory regions. I am not completely sure, but some compilers might even produce code that will segfault on such circumstance. Even if that's not the case, it's not something I like to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...