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.
/*
* C++ Program to Implement Sorted 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->data < np->data))
{
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)
{
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++;
}
cout<<"\n\nlinear display of nodes currently present in circularly linked list....\n\n";
traverse(n);
getch();
}
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
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Data Structure Books
- Check Programming Books
- Practice Programming MCQs
- Practice Computer Science MCQs
- Practice Design & Analysis of Algorithms MCQ