This C++ program displays the nodes of a singly linked list with the last node pointing back to the first giving a circularly linked list.

Here is the source code of the C++ program to display the values present in the nodes cyclically. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

`/*`

`* C++ Program to Implement Sorted Circularly Singly Linked List`

`*/`

`#include<stdio.h>`

`#include<conio.h>`

`#include<iostream>`

using namespace std;

`struct node`

`{`

int data;

node *next;

}*p = NULL, *head = NULL, *q = NULL, *np = NULL;

int c = 0;

void create(int x)

`{`

np = new node;

np->data = x;

np->next = NULL;

if (c == 0)

`{`

head = np;

p = head;

p->next = head;

c++;

`}`

else if (c == 1)

`{`

p = head;

q = p;

if (np->data < p->data)

`{`

np->next = p;

head = np;

p->next = np;

`}`

else if (np->data > p->data)

`{`

p->next = np;

np->next = head;

`}`

c++;

`}`

`else`

`{`

p = head;

q = p;

if (np->data < p->data)

`{`

np->next = p;

head = np;

`do`

`{`

p = p->next;

`}`

while (p->next != q);

p->next = head;

`}`

else if (np->data > p->data)

`{`

while (p->next != head && q->data < np->data)

`{`

q = p;

p = p->next;

if (p->next == head)

`{`

p->next = np;

np->next = head;

`}`

else if (np->data < p->data)

`{`

q->next = np;

np->next = p;

break;

`}`

`}`

`}`

`}`

`}`

void traverse(int i)

`{`

node *t = head;

int c = 0;

while (c <= i + 1)

`{`

cout<<t->data<<"\t";

t = t->next;

c++;

`}`

`}`

int main()

`{`

int i = 0, n, x;

cout<<"enter the no of nodes\n";

cin>>n;

while (i < n)

`{`

cout<<"\nenter value of node\n";

cin>>x;

create(x);

i++;

`}`

getch();

`}`

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

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

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