Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help with linked list
Message
From
26/04/2002 04:51:32
 
 
To
25/04/2002 16:01:54
Stephen Hunt
Admit Computer Services Inc.
Farmingdale, New York, United States
General information
Forum:
Visual C++
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00649301
Message ID:
00649500
Views:
6
>I am looking for some help with understanding double linked list. If possible could I see a sample program from start to finish.

Can you be a bit more specific about what you are trying to understand ?

A double linked list is really no more complex than a singly linked list. A singly linked list has a pointer to the next item in the list, a double linked list has two pointers, one to the next item in the list & one to the preceding item in the list.

When traversing a list, with a singly linked list you can only move through the list in one direction following the next item pointer, with a doubly linked list you can move in either direction following the next or previous item pointer.

Inserting items into the list, you have to set both the next item & previous item pointer of the new item & then the appropriate pointers of the items on either side of the inserted item. With a singly linked list, it is only possible to insert after a selected item (insert before is done by searching for the item that points to the item you want to insert before & then insert after that one). With a doubly linked list, insert before & insert after options are easy.

Below is an example class with a few of the things you might want to do, it was written off the top of my head, so it doesn't do everything. Hope it helps.
class xyz
{
public :
  xyz *GetNextItem() { return next ; }
  xyz *GetPrevItem() { return prev ; }

  void InsertAfter( xyz *NewItem )  // Insert NewItem after this item in list
    {
    NewItem->prev = this ;   // item prior to NewItem will be this one
    NewItem->next = next ;   // item next to NewItem will be the one currently next to this one
    if ( next != NULL )      // if not last item in list
      next->prev = NewItem ; // next in list wants to point back to new item
    next = NewItem ;         // next item to this will be the NewItem
    }

  void RemoveFromList()            // Remove this item from the list it is in
    {                              // CAREFUL USING THIS - MAY CAUSE MEMORY LEAK IF NO POINTER TO THE ITEM AFTER REMOVED
    if ( next != NULL )      // if not last item in list
      next->prev = prev ;    // item before next in list will be the one prior to this one
    if ( prev != NULL )      // if not first item in list
      prev->next = next ;    // item after previous in list will be the one next to this one
    next = NULL ;            // set pointers of this item to NULL
    prev = NULL ;
    }

  xyz *RemovePreviousItem()        // Remove previous item in list, returning a pointer to it
    {
    xyz *RemovedItem = prev ;    // save pointer to previous item in list
    if ( prev != NULL )          // if there is an item
      prev->RemoveFromList() ;   // get it to remove itself from the list
    return RemovedItem ;         // return pointer to removed item
    }

private :
  xyz *next ;
  xyz *prev ;
}
Len Speed
Previous
Reply
Map
View

Click here to load this message in the networking platform