Triply linked list














































Triply linked list



This C++ program implements the Triply Linked List which is a linked list which consists of three pointers which points to the element at the next and previous to it in addition to the element at the top.

Here is the source code of the C++ program to display the sorted triply linked list by travelling head first and tail first. This C++ program is successfully compiled and run on Dev Cpp, a C++ compiler. The program output is given below.


  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<iostream.h>
  4. using namespace std;
  5. int z = 0;
  6. struct node
  7. {
  8.     node *next, *prev, *top;
  9.     int data;
  10. }*head = NULL, *tail = NULL, *p = NULL, *r = NULL, *np = NULL, *q = NULL;
  11. void create(int x)
  12. {
  13.     np = new node;
  14.     np->data = x;
  15.     np->next = NULL;
  16.     np->prev = NULL;
  17.     np->top = NULL;
  18.     if (z == 0)
  19.     {
  20.         tail = np;
  21.         head = np;
  22.         p = head;
  23.         p->next = NULL;
  24.         p->prev = NULL;
  25.         p->top = NULL;
  26.         z++;
  27.     }
  28.     else
  29.     {
  30.         p = head;
  31.         r = p;
  32.         if (np->data < p->data)
  33.         {
  34.             np->next = p;
  35.             p->prev = np;
  36.             np->prev = NULL;
  37.             head = np;
  38.             p = head;
  39.             do
  40.             {
  41.                 p = p->next;
  42.             }
  43.             while (p->next != NULL);
  44.             tail = p;
  45.         }
  46.         else if (np->data > p->data)
  47.         {
  48.             while (p != NULL && np->data > p->data)
  49.             {
  50.                 r = p;
  51.                 p = p->next;
  52.                 if (p == NULL)
  53.                 {
  54.                     r->next = np;
  55.                     np->prev = r;
  56.                     np->next = NULL;
  57.                     tail = np;
  58.                     break;
  59.                 }   
  60.                 else if (np->data <= p->data)
  61.                 {
  62.                     if (np->data < p->data)
  63.                     { 
  64.                         r->next = np;
  65.                         np->prev = r;
  66.                         np->next = p;
  67.                         p->prev = np;
  68.                         if (p->next != NULL)
  69.                         {
  70.                             do
  71.                             {
  72.                                 p = p->next;
  73.                             }
  74.                         while (p->next !=NULL);
  75.                     }
  76.                     tail = p;
  77.                     break;
  78.                     }
  79.                     else if (p->data == np->data)
  80.                     {
  81.                         q = p;
  82.                         while (q->top != NULL)
  83.                         {
  84.                            q = q->top;
  85.                         }
  86.                         q->top = np;
  87.                         np->top = NULL;
  88.                         break;
  89.                     }
  90.                 }
  91.             }
  92.         }        
  93.     }
  94. }
  95. void traverse_tail()
  96. {
  97.     node *t = tail;
  98.     //cout<<"\n\nlinear display of nodes currently present in linked list....\n\n";
  99.     while (t != NULL)
  100.     {
  101.         cout<<t->data<<"\t";
  102.         q = t;
  103.         while (q->top != NULL)
  104.         {
  105.               q = q->top;
  106.               cout<<"top->"<<q->data<<"\t";
  107.         }
  108.         t = t->prev;
  109.     }
  110.     cout<<endl;
  111. }
  112. void traverse_head()
  113. {
  114.     node *t = head;
  115.     while (t != NULL)
  116.     {
  117.         cout<<t->data<<"\t";
  118.         q = t;
  119.         while (q->top != NULL)
  120.         {
  121.             q = q->top; 
  122.             cout<<"top->"<<q->data<<"\t";
  123.         }
  124.         t = t->next;
  125.     }
  126.     cout<<endl;
  127. }
  128. int main()
  129. {
  130.     int i = 0, n, x, ch;
  131.     cout<<"Enter the no of nodes\n";
  132.     cin>>n;
  133.     while (i < n)
  134.     {
  135.         cout<<"\nEnter value of node\n";
  136.         cin>>x;
  137.         create(x);
  138.         i++;
  139.     }
  140.     cout<<"\nTraversing Doubly Linked List head first\n";
  141.     traverse_head();
  142.     cout<<"\nTraversing Doubly Linked List tail first\n";
  143.     traverse_tail();
  144.     getch();
  145. }
  146. Output
     
    enter the no of nodes
    6
     
    enter value of node
    2
     
    enter value of node
    6
     
    enter value of node
    3
     
    enter value of node
    5
     
    enter value of node
    3
     
    enter value of node
    8
     
    Traversing Doubly Linked List head first
    2       3       top->3  5       6       8
     
    Traversing Doubly Linked List tail first
    8       6       5       3       top->3  2

More Articles of Rupsi Kumari:

Name Views Likes
C++Boost Operators 239 0
Triply linked list 1386 1
C++Boost::operators 277 0

Comments