Thread: double linked lists

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Unhappy double linked lists

    i am having difficulty writting a function that adds and deletes a node from a double linked list. could someone please help me out.
    i am making use of pointers.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    a doubly linked list looks like this.....
    Code:
    NULL <--root(node0) <----> node1<---->node2<---->node3---->NULL
    by this i mean that root has a pointer to null and a pointer to node1
    node1 has a pointer to node 2 but also has a pointer to root.

    if we wanted to delete node1....

    node2 pointer to node 1 would have to point to root
    root pointer to node1 would be altered to point to node2
    Dont forget to free memory associated with node1. You now have this sort of structure.
    Code:
    NULL <--root<---->node2<---->node3---->NULL
    If we wanted to add a new node lets call it node4 back where node1 used to be....

    root pointer to node2 altered to point to node4
    node4 pointer(prev.) set to root
    node4 pointer(next) set to node2
    node2 pointer to root altered to point to node4.

    ending up with....
    Code:
    NULL <--root <----> node4<---->node2<---->node3---->NULL
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest

    Thumbs up Interesting

    Pretty good explanaition of linked lists there.

    But do you (or anyone else) know of any GOOD tut's or books that deal with linked lists. I've read a couple of them including the short one on this site and understand the logic behind deleting and adding nodes but would like to see a few examples of how different people implement this in code.

    Im about to write a small prog for storing records in a binary file for my class.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    do a search at google for linked lists. search for a c implementation to see the code properly. In c++ linked lists are normally done a little different by using the stl.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 04:25 AM
  2. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. Replies: 1
    Last Post: 03-21-2002, 06:10 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM