Software & Finance





Turbo C++ - Doubly Linked List





I have given here Turbo C++ sample code for doubly linked list, filling elements and traversal in forward and backward direction. I have NOT given the code for deleting the nodes. It is easy to implement by changing the insert functions.

 

Singly Linked List, Doubly Linked List, Singly Linked Circular List and Doubly Linked Circular List are an important data structure used in C++ applications.

Related Links:



Source Code


// Program for doubly linked list

#include <iostream.h>

 

 

struct DLinkList

{

       int data;

 

       DLinkList *next;

       DLinkList *prev;

 

       DLinkList();

       DLinkList(int value);

 

       DLinkList* InsertNext(int value);

       DLinkList* InsertPrev(int value);

       void TraverseFront(DLinkList *node = NULL);

       void TraverseBack(DLinkList *node = NULL);

};

 

DLinkList::DLinkList() : data(0), next(NULL), prev(NULL) { }

 

DLinkList::DLinkList(int value) : data(value), next(NULL), prev(NULL) { }

 

DLinkList* DLinkList::InsertNext(int value)

{

       DLinkList *node = new DLinkList(value);

       if(this->next == NULL)

       {

 

              // Easy to handle

              node->prev = this;

              node->next = NULL; // already set in constructor

              this->next = node;

       }

       else

       {

              // Insert in the middle

              DLinkList *temp = this->next;

              node->prev = this;

              node->next = temp;

              this->next = node;

              temp->prev = node;

              // temp->next does not have to be changed

       }

       return node;

}

 

DLinkList* DLinkList::InsertPrev(int value)

{

       DLinkList *node = new DLinkList(value);

       if(this->prev == NULL)

       {

              node->prev = NULL; // already set on constructor

              node->next = this;

              this->prev = node;

       }

       else

       {

 

              // Insert in the middle

              DLinkList *temp = this->prev;

              node->prev = temp;

              node->next = this;

              this->prev = node;

              temp->next = node;

              // temp->prev does not have to be changed

       }

       return node;

}

 

void DLinkList::TraverseFront(DLinkList *node)

{

       if(node == NULL)

              node = this;

       cout << "\n\nTraversing in Forward Direction\n\n";

 

       while(node != NULL)

       {

              cout << node->data;

              cout << "\n";

              node = node->next;

       }

}

 

void DLinkList::TraverseBack(DLinkList *node)

{

       if(node == NULL)

              node = this;

       cout << "\n\nTraversing in Backward Direction\n\n";

       while(node != NULL)

       {

              cout << node->data;

              cout << "\n";

              node = node->prev;

       }

}

 

int main()

{

       DLinkList *node1 = new DLinkList(1);

       DLinkList *node3 = node1->InsertNext(3);

       DLinkList *node2 = node3->InsertPrev(2);

       DLinkList *node5 = node3->InsertNext(5);

       DLinkList *node4 = node5->InsertPrev(4);

 

       node1->TraverseFront();

       node5->TraverseBack();

 

       cout << "\n";

       return 0;

}

 

 

Output


Traversing in Forward Direction

1
2
3
4
5

 

Traversing in Backward Direction

5
4
3
2
1