What is a Circular Linked List?
A circular linked list is a type of linked list where the last node of the list
points back to the first node instead of pointing to NULL. This circular linking
makes traversal of the list from any point possible and eliminates the need to
maintain a separate pointer for the head and tail of the list.
In a Circular linked list, every element has a link to its next element in the
sequence, and the last element has a link to the first element. A circular linked
list is similar to the singly linked list except that the last node points to the
first node
Representation of Circular Linked List in C
In C, a circular linked list is represented using a Node structure that contains
data and a pointer to the next node:
struct Node {
int data;
struct Node* next;
};
1. Insertion at the beginning:
Insert a new node as the first node. The next pointer of last will point to this
node and this new node will point to the previous first node.
Below is the implementation of the above operation:
Basic Structure for Circular Linked List:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node *next;
};
struct node *last=NULL;
void insert_at_beg()
struct node *newnode;
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the Data value:");
scanf("%d",&key);
newnode->data=key;
if(last==NULL)// if no any node present
newnode->next=newnode ;
last=newnode;
}
else //if last node already present
newnode->next=last->next;
last->next=newnode;
printf(" \n Node added at Beginning:");
2. Insertion at the end:
Inserting a new node as the last node. The next pointer of last will point to this
node and this new node will point to the first node.
Below is the implementation of the above operation:
void insert_at_end()
struct node *newnode;
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the Data value:");
scanf("%d",&key);
newnode->data=key;
if(last==NULL)// if no any node present
newnode->next=newnode ;
last=newnode;
}
else
newnode->next = last->next;
last->next = newnode;
last = newnode;
printf(" \n Node added at end:");
3. Insertion after a specific element:
Below is the program to insert a node after a specified node in the linked list:
// C program for the above operation
void insert_at_position()
int pos;
int key;
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the Data value:");
scanf("%d",&key);
newnode->data=key;
printf("\n Enter the Position Where you wann add node:");
scanf("%d",&pos);
struct node *temp;
temp=last->next;
for(int i=1;i<pos-1;i++)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
printf(" \n Node added at given Position:");
4. Delete the first element:
Deleting the first node of the linked list. For this, the next pointer of the last
will point to the second node of the linked list.
Below is the implementation of the above operation:
void delete_at_start()
if(last==NULL)
printf("linked List is Empty");
exit(0);
struct node *head;
head=last->next;
if(head==last)// if only single node present
// head=NULL;
free(last);
else
head=head->next;
last->next=head;
free(head);
printf("\n First Node is deleted:\n");
5. Delete the last element:
Deleting the last node of the linked list. For this, the second last node will point
to the first node of the list.
Below is the implementation of the above operation:
void delete_at_end()
if(last==NULL)
printf("linked List is Empty");
exit(0);
}
struct node *head;
head=last->next;
if(head==last)// if only single node present
// head=NULL;
free(last);
else
struct node * temp;
temp=last->next;
while(temp->next !=last)// Traverse upto 2 nd last node
temp=temp->next;
temp->next = last->next;
last = temp;
printf(" \n Last Node is deleted:\n");
6. Delete at a given position:
Delete an element from a specified position in the linked list
void delete_at_pos()
struct node *temp;
temp=last->next;
int loc;
printf("\n enter the Position where u wann Delete Node:");
scanf("%d",&loc);
for(int i=1;i<loc-1;i++)
temp=temp->next;
temp->next=temp->next->next; // skip the node
printf("\n node deleted at Location %d:", loc);
Display the CLL:
void display_cll()
struct node *temp;
temp=last->next; // here last->next means first node and assigned
in temp
if(last==NULL)
{
printf("List is Empty");
exit(0);
printf("\n List Elements are:\n");
do
printf("%d-->",temp->data);
temp=temp->next;
}while(temp!=last->next);
printf("NULL\n");
Search in Circular Linked List:
void search_cll()
struct node *temp;
temp=last->next;
int item;
printf("\n Enter the Searched Data Item Value:\n");
scanf("%d",& item);
do
{
if(temp->data==item)
printf("Item present in SLL");
exit(0);
temp=temp->next;
}while(temp!=last->next);
printf("Item not Found in SLL");
void main()
insert_at_beg();
insert_at_end();
insert_at_position();
display_cll();
delete_at_start();
display_cll();
delete_at_end();
display_cll();
delete_at_pos();
display_cll();
search_cll();
}