What is Circular Linked List? Add in Start,Add in End of Circular Linked List

Circular Linked List :Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.

Singly Linked List as Circular

In singly linked list, the next pointer of the last node points to the first node.

Doubly Linked List as Circular

In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions.
As per the above illustration, following are the important points to be considered.

The last link’s next points to the first link of the list in both cases of singly as well as doubly linked list.

Basic Operations

Following are the important operations supported by a circular list.

insert − Inserts an element at the start of the list.

delete − Deletes an element from the start of the list.

display − Displays the list.

Application of Circular Linked List

1 – The real life application where the circular linked list is used is our Personal Computers, where multiple applications are running. All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the linked list until all the applications are completed.
2 – Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on moving forward as a player’s chance ends.
3 – Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two pointers, FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer is required.

Add in start of Circular Linked List

void add_start() {
if(start==NULL)
{ temp=new list;
cout<<"Enter Data:";
cin>>temp->data;
temp->next=temp;
start=temp;
}
else
{
temp=new list;
cout<<"Enter Data: ";
cin>>temp->data;
for(temp1=start;temp1->next!=start;temp1=temp1->next)
{}
temp1->next=temp;
temp->next=start;
start=temp; }}

Add in End in Circular Linked List

void add_end() {
if(start==NULL)
{
temp=new list;
cout<<"Enter Data: ";
cin>>temp->data;
temp->next=temp;
start=temp;}
else
{
temp=new list;
cout<<"Enter Data: ";
cin>>temp->data;
temp->next=start;
for(temp1=start;temp1->next!=start;temp1=temp1->next)
{}
temp1->next=temp;
}
}

Remove From End in Circular Linked List

void remove_end()
{
if(start==NULL)
{
cout<<"empty"<<endl;
}
else
{
if(start->next==start)
{
start=NULL;
}
else{
for(temp=start;temp->next!=start;temp=temp->next)
{
temp1=temp;
}
temp1->next=start;
}}}

Remove From start of Circular Linked List

void remove_start()
{
if(start==NULL)
{
cout<<"empty"<<endl;
}
else{
if(start->next==start)
{
start=NULL;
}
else
{
for(temp=start;temp->next!=start;temp=temp->next)
{}
temp->next=start->next;
start=start->next; }}}

Update Number in Circular Linked List

void update()
{
if(start==NULL)
cout<<"empty"<<endl;
else
{
int n,x=0;
cout<<"Enter Number which you wana update: ";
cin>>n;
for(temp=start;temp->next!=start;temp=temp->next)
{
if(temp->data==n)
{
cout<<"Enter New Number";
cin>>temp->data;
x++; }}
if(x==0)
cout<<"No Number Found"<<endl;}}

Remove From Specific Location in Circular Linked List

void by_loc()
{
if(start==NULL)
cout<<"Empty"<<endl;
else{
int l,x=0;
cout<<"Enter Location: ";
cin>>l;
if(l==1)
{for(temp=start;temp->next!=start;temp=temp->next)
{}
temp->next=start->next;
start=start->next;
}
else
{
temp=start;
for(int i=2;temp->next!=start;i++)
{
temp1=temp;
temp=temp->next;
if(i==l)
{
temp1->next=temp->next;
}} }}}

Display function of Circular Linked List

void show()
{
system("cls");
if(start==NULL)
cout<<"Empty"<<endl;
else
{
for(temp=start;temp->next!=start;temp=temp->next)
cout<<temp->data<<endl;
cout<<temp->data<<endl;
} system("pause");
system("cls");}