Program in c++ to create, insert and delete in circular linked list.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*last;
class circular_llist
{
public:
void create_node(int value);
void add_after(int value,int position);
void delete_element(int value);
void display_list();
circular_llist()
{
last=NULL;
}
};
int main()
int
choice,element,posittion;
circular_llist cl;
while(1)
{
cout<<"\n
-------------------------------------------------------------";
cout<<"\n circular singly linked
list";
cout<<"\n------------------------------------------------------------";
cout<<"\n
1.create node";
cout<<"\n
2.insert node";
cout<<"\n
3.delete node";
cout<<"\n
4.display";
cout<<"\n
5.quit";
cout<<"\n enter your choice:";
cin>>choice;
switch(choice)
{
case1:
cout<<"enter
the element:";
cin>>element;
cl.create_node(element);
cout<<endl;
break;
case2:
cout<<"enter
the element:";
cin>>element;
cout<<'insert
element after position:";
cin>>position;
cl.add_after(element,position);
cout<<endl;
break;
case3:
if(last==NULL)
{
cout<<"list is empty,nothing
to delete"<<endl:";
break;
}
cout<<"enter
the element for deletion:";
cin>>element;
cl.delete_element(element);
cout<<endl;
break;
case4:
cl.display_list();
break;
case5:
exit(1);
break;
default:
cout<<"wroung
choice"<<endl;
}
}
}
void
circular_llist::create_node(int value)
{
struct node*temp;
temp=new(struct
node)
temp->info=value;
if(last==NULL)
{
last=temp;
temp->next=last;
}
else
{
temp->next=last->next;
last->next=temp;
last=temp;
}
}
void
circular_llist::add_after(int value,int pos)
{
if(last==NULL)
{
cout<<"first
create the list,"<<endl;
return;
}
struct node*temp,*s;
s=last->next;
for(int
i=0;i<pos-1;i++)
{
s=s->next;
if(s==last->next)
{
cout<<"there
are less than";
return;
}
}
temp=new(struct
node);
temp->next=s->next;
temp->info=value;
s->next=temp;
/*element inserted
at the end*/
if(s==last)
{
last=temp;
}
}
void
circular_llist::delete_element(int value)
{
struct node*temp,*s;
s=last->next;
/*if list has only
one element*/
if(last->next==last
&& last->info==value)
{
temp=last;
last=NULL;
free(temp);
return;
}
if(s->info==value)/*first
element deletion*/
{
temp=s;
last->next=s->next;
free(temp)
return;
}
while(s->next!=last)
{
/*deletion of
element in between*/
if(s->next->info==value)
{
temp=s->next;
s->next=temp->next;
free(temp);
cout<<"element"<<value;
cout<<"deleted
from the list"<<endl;
return;
}
s=s->next;
}
/*deletion of last
element*/
if(s->next->info==value)
{
temp=s->next;
s->next=last->next;
free(temp);
last=s;
return;
}
cout<<"element"<<value<<"not
found in the list"<<endl;
}
void
circular_llist::display_list()
{
struct node*s;
if(last==NULL)
{
cout<<"list
is empty,nothing to display"<<endl;
return;
}
s=last->next;
cout<<"circular
link list:"<<endl;
while(s!=last)
{
cout<<s->info<<"->;
s=s->next;
}
cout<<s->->info<<endl;
}
output:
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:1
enter the element:10
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:1
enter the element:20
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:1
enter the element:30
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:1
enter the element:40
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:3
circular linked
list:
10->20->30->40
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:2
enter the element:25
insert element after
position:2
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:3
circular linked
list:
10->20->25->30->40
-----------------------
circular single
linked list
-------------------------------------
1.create node
2.insert node
3.quit
enter your choice:4