You are on page 1of 9

Circular linked list

Circular linked list


• Circular linked list is a linear data structure.
• In Circular linked list the last node is connected back to the
first node to make a circle.
1. It can be made to follows First In First Out principle if
a. Elements are added at the rear end and the elements are
deleted at front end of the queue
2. Both the front and the rear pointers points to the
memory location.
3. It is also called as “Ring buffer”.
• Circular linked list can be created in three ways they are
1.  Using single linked list
2.  Using double linked list
3.  Using arrays
Circular linked list
•Using single linked list:
•It is an extension for the basic single linked list.
•In circular linked list Instead of storing a Null value in the last
node of a single linked list, store the address of the 1st node
(root) to forms a circular linked list.
•Using circular linked list it is possible to directly traverse to
the first node after reaching the last node.
•Using double linked list:
•In double linked list the right side pointer points to the next
node address or the address of first node and
•left side pointer points to the previous node address or the
address of last node of a list.
•Hence the above list is known as circular double linked list.
Circular Double Linked List
Circular linked list
#include<iostream.h> /* Main :contains menu */
//using namespace std; int main()
class circular_llist {
int choice, element, position;
{
circular_llist cl, *l;
int info; //to store data
do
circular_llist *next, *last; //self ref {
public: //cout<<endl<<"---------------------------"<<endl;
void create_node(int value); cout<<endl<<"Circular singly linked
void add_begin(int value); list"<<endl;
void display_list(); //cout<<endl<<"---------------------------"<<endl;
circular_llist() cout<<"1.Create Node"<<endl;
cout<<"2.Add at beginning"<<endl;
{
cout<<"3.Display"<<endl;
last = NULL;
cout<<"9.Quit"<<endl;
} cout<<"Enter your choice : ";
}; cin>>choice;
Circular linked list
switch(choice) /* Create very first Circular Link List */
{
void circular_llist::create_node(int value)
case 1:
cout<<“enter first element: "; {
cin>>element; circular_llist *temp;
cl.create_node(element); temp = new(circular_llist);
cout<<endl;
break;
temp->info = value;
case 2: if (last == NULL)
cout<<“enter the element: "; {
cin>>element;
last = temp;
cl.add_begin(element);
cout<<endl; temp->next = last;
break; }
case 3: else
cl.display_list();
break;
{
case 9: temp->next = last->next;
break; last->next = temp;
default:
last = temp;
cout<<"Wong choice"<<endl;
} }
}while (choice >=0 && choice <=8); }
return 0;
}
Circular linked list
/* Insertion of element at beginning */ /* Display Circular Link List */
void circular_llist::add_begin(int value) void circular_llist::display_list()
{
{
if (last == NULL)
{ circular_llist *s;
cout<<"First Create the list."<<endl; if (last == NULL)
return; {
} cout<<"List is empty, nothing to display"<<endl;
circular_llist *temp;
return;
temp = new(circular_llist);
temp->info = value; }
temp->next = last->next; s = last->next;
last->next = temp; cout<<"Circular Link List: "<<endl;
} while (s != last)
{
cout<<s->info<<"->";
s = s->next;
}
cout<<s->info<<endl;
}
Advantages and Disadvantages of Circular Linked List
• Advantages
• Last node link has address instead of NULL value.
• We can visit all nodes starting from any point.
• It is a special type of linked list.
• Can be used for applications where a sequence of
steps are required to be taken repetitively.
• Disadvantage
• If the programming is not carefully done it can lead
to infinite loop.

You might also like