Techno World Inc - The Best Technical Encyclopedia Online!

THE TECHNO CLUB [ TECHNOWORLDINC.COM ] => C/C++/C# => Topic started by: Taruna on December 27, 2006, 12:37:27 AM



Title: C/C++ Doubly Linked List
Post by: Taruna on December 27, 2006, 12:37:27 AM
The implementation of a doubly linked list, remove prev pointer for a singly linked list

Code:
struct Node{ 
public:
   Member*      info; //type is arbitrary, to the linked list implementation
   Node*      next; //used to point to next node
        Node*           prev; //used to point to previous node

   Node():prev(NULL),next(NULL) {} //default constructor
        Node(Node* n):prev(n),next(NULL) {} //prev known constructor
        Node(Node* n, Node* m):prev(n),next(m) {} //prev and next known constructor
   ~Node() { delete info; delete next; } //destructor
};

this list would be held by a pointer:

Code:
Node* head; 
head = &(new Node(&head);//create Node which knows prev(head) and next is NULL

then each node is attatched with a similar fasion.

To search such a list use a while loop:

Code:
Node* n = head; 
while(n->next){
//loop code
n = n->next;
}

Questions/Comments: [email protected]