Below a simple program is provided that can take input continuously unless user input is -5.Each element will be stored in circular linked list.Once user entered -5, linked list will be closed and then displayed to the user.
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *link;
}*head=NULL;
void create()
{
node *new1=NULL;
node *end=new1;
cout<<"Enter -5 to terminate link list:"<<endl;
while(1)
{
int info;
cout<<"Enter data: ";
cin>>info;
if(info==-5)
break;
new1=new node;
new1->data=info;
if(head==NULL)
head=new1;
else
end->link=new1;;
end=new1;
end->link=head;
}
}
void display()
{
node *p=head;
cout<<"\n\nYour output is:";
while(p->link!=head)
{
cout<<" "<<p->data;
p=p->link;
}
cout<<" "<<p->data;
}
main()
{
cout<<"\t***Simple circle link list***"<<endl;
create();
display();
getch();
}
Output of program:
C++ , Linked List