C++ Program to Implement 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 DevCpp, a C++ compiler. The program output is given below.

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

advertisement
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

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn