C++ Program to Implement Sorted Singly Linked List

«
»
This C++ program displays the nodes of a singly linked list in which linked list can be traversed from the first node to last node.

Here is the source code of the C++ program to display the values present in the nodes from the head to the last node. 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 Sorted Singly Linked List
  3.  */
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<iostream>
  7. using namespace std;
  8. struct node
  9. {
  10.     int data;
  11.     node *next;
  12. }*p = NULL, *head = NULL, *q = NULL, *np = NULL;
  13. int c = 0;
  14. void create(int x)
  15. {
  16.     np = new node;
  17.     np->data = x;
  18.     np->next = NULL;
  19.     if (c == 0)
  20.     {
  21.         head = np;
  22.         p = head;
  23.         p->next = head;
  24.         c++;
  25.     }
  26.     else if (c == 1)
  27.     {
  28.         p = head;
  29.         q = p;
  30.         if (np->data < p->data)
  31.         {
  32.             np->next = p;
  33.             head = np;
  34.             p->next = np;
  35.         }
  36.         else if (np->data > p->data)
  37.         {
  38.             p->next = np;
  39.             np->next = head;
  40.         }
  41.         c++;
  42.     }
  43.     else
  44.     {
  45.         p = head;
  46.         q = p;
  47.         if (np->data < p->data)
  48.         {
  49.             np->next = p;
  50.             head = np;
  51.             do
  52.             {
  53.                 p = p->next;
  54.             }
  55.             while (p->next != q);
  56.             p->next=head;
  57.         }
  58.         else if (np->data > p->data)
  59.         {
  60.             while (p->next !=head && q->data < np->data)
  61.             {
  62.                 q = p;
  63.                 p = p->next;
  64.                 if (p->next == head  && (p->data < np->data))
  65.                 {
  66.                     p->next = np;
  67.                     np->next = head;
  68.                 }
  69.                 else if (np->data < p->data)
  70.                 { 
  71.                     q->next = np;
  72.                     np->next = p;
  73.                     break;
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
  79. void traverse(int i)
  80. {
  81.     node *t = head;
  82.     int c = 0;
  83.     while (c <= i)
  84.     {
  85.         cout<<t->data<<"\t";
  86.         t = t->next;
  87.         c++;
  88.     }
  89. }
  90. int main()
  91. {
  92.     int i = 0, n, x;
  93.     cout<<"enter the no of nodes\n";
  94.     cin>>n;
  95.     while (i < n)
  96.     {
  97.         cout<<"\nenter value of node\n";
  98.         cin>>x;
  99.         create(x);
  100.         i++;
  101.     }
  102.     cout<<"\n\nlinear display of nodes currently present in circularly linked list....\n\n";
  103.     traverse(n);
  104.     getch();
  105. }

advertisement
 
Output
 
enter the no of nodes
7
 
enter value of node
4
 
enter value of node
6
 
enter value of node
1
 
enter value of node
9
 
enter value of node
3
 
enter value of node
8
 
enter value of node
2
 
 
linear display of nodes currently present in circularly linked list....
 
1       2       3       4       6       8       9       1

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

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

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
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 | Youtube | Instagram | Facebook | Twitter