Circular Linked List Operations
Circular Linked List Operations
In a circular Singly linked list, the last node of the list contains a pointer to the first node of the list. We
can have circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started. The circular
singly liked list has no beginning and no ending. There is no null value present in the next part of any of
the nodes.
Circular linked list are mostly used in task maintenance in operating systems. There are many examples
where circular linked list are being used in computer science including browser surfing where a record of
pages visited in the past by the user, is maintained in the form of circular linked lists and can be
accessed again on clicking the previous button.
In the following image, memory representation of a circular linked list containing marks of a student in 4
subjects. However, the image shows a glimpse of how the circular list is being stored in the memory. The
start or head of the list is pointing to the element with the index 1 and containing 13 marks in the data
part and 4 in the next part. Which means that it is linked with the node that is being stored at 4th index
of the list.
However, due to the fact that we are considering circular linked list in the memory therefore the last
node of the list contains the address of the first node of the list.
We can also have more than one number of linked list in the memory with the different start pointers
pointing to the different start nodes in the list. The last node is identified by its next part which contains
the address of the start node of the list. We must be able to identify the last node of any linked list so
that we can find out the number of iterations which need to be performed while traversing the list.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
while(choice != 7)
printf("\n*********Main Menu*********\n");
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
void beginsert()
int item;
if(ptr == NULL)
{
printf("\nOVERFLOW");
else
scanf("%d",&item);
if(head == NULL)
head = ptr;
else
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
head = ptr;
printf("\nnode inserted\n");
}
void lastinsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
else
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
else
temp = head;
printf("\nnode inserted\n");
void begin_delete()
if(head == NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{ ptr = head;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
void last_delete()
if(head==NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{
ptr = head;
preptr=ptr;
ptr = ptr->next;
free(ptr);
printf("\nnode deleted\n");
void search()
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
flag=0;
else
if(ptr->data == item)
flag=0;
break;
else
flag=1;
i++;
}
if(flag != 0)
void display()
ptr=head;
if(head == NULL)
printf("\nnothing to print");
else
Output:
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit
Enter Data?20
node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit
Enter Data?30
node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit
5
Enter item which you want to search?
20
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit
20
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
6.Show
7.Exit