Program for all operations on Circular Linked List in C
Last Updated :
27 Apr, 2023
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. Below is the image to illustrate the same:

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:
C
// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in the list
struct node* last = NULL;
// Function to insert a node in the
// starting of the list
void insertAtFront(int data)
{
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// If the new node is the only
// node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else last node contains the
// reference of the new node and
// new node contains the reference
// of the previous first node
else {
temp->info = data;
temp->next = last->next;
// last node now has reference
// of the new node temp
last->next = temp;
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
// While first node is not
// reached again, print,
// since the list is circular
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Function Call
insertAtFront(10);
insertAtFront(20);
insertAtFront(30);
// Print list
viewList();
return 0;
}
OutputData = 30
Data = 20
Data = 10
The time complexity of the insertAtFront function is O(1) as it performs a constant amount of work to insert a node at the front of the linked list.
The time complexity of the viewList function is O(n) as it has to traverse the entire linked list to print its elements.
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:
C
// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in the list
struct node* last = NULL;
// Function to add a new node at the
// end of the list
void addatlast(int data)
{
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// If the new node is the
// only node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be the
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Function Call
addatlast(10);
addatlast(20);
addatlast(30);
// Print list
viewList();
return 0;
}
OutputData = 10
Data = 20
Data = 30
3. Insertion after a specific element:
Below is the program to insert a node after a specified node in the linked list:
C
// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
// If the new node is the
// only node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be the
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to insert after any
// specified element
void insertafter()
{
// Stores data and element after
// which new node is to be inserted
int data, value;
// Initialize a new node
struct node *temp, *n;
// Input data
printf("\nEnter number after which"
" you want to enter number: \n");
scanf("%d", &value);
temp = last->next;
do {
// Element after which node is
// to be inserted is found
if (temp->info == value) {
n = (struct node*)malloc(sizeof(struct node));
// Input Data
printf("\nEnter data to be"
" inserted : \n");
scanf("%d", &data);
n->info = data;
n->next = temp->next;
temp->next = n;
// If temp is the last node
// so now n will become the
// last node
if (temp == last)
last = n;
break;
}
else
temp = temp->next;
} while (temp != last->next);
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast();
addatlast();
addatlast();
// Function Call
insertafter();
// Print list
viewList();
return 0;
}
Output:

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:
C
// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast(int data)
{
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// If the new node is the only
// node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be the
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to delete the first
// element of the list
void deletefirst()
{
struct node* temp;
// If list is empty
if (last == NULL)
printf("\nList is empty.\n");
// Else last node now contains
// reference of the second node
// in the list because the
// list is circular
else {
temp = last->next;
last->next = temp->next;
free(temp);
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast(10);
addatlast(20);
addatlast(30);
printf("Before deletion:\n");
viewList();
// Function Call
deletefirst();
// Print list
printf("\n\nAfter deletion:\n");
viewList();
return 0;
}
OutputBefore deletion:
Data = 10
Data = 20
Data = 30
After deletion:
Data = 20
Data = 30
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:
C
// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast(int data)
{
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// If the new node is the only
// node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to delete the last node
// in the list
void deletelast()
{
struct node* temp;
// If list is empty
if (last == NULL)
printf("\nList is empty.\n");
temp = last->next;
// Traverse the list till
// the second last node
while (temp->next != last)
temp = temp->next;
// Second last node now contains
// the reference of the first
// node in the list
temp->next = last->next;
last = temp;
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast(10);
addatlast(20);
addatlast(30);
printf("Before Deletion:\n");
viewList();
// Function Call
deletelast();
// Print the list
printf("\n\nAfter Deletion:\n");
viewList();
return 0;
}
OutputBefore Deletion:
Data = 10
Data = 20
Data = 30
After Deletion:
Data = 10
Data = 20
6. Delete at a given position:
Delete an element from a specified position in the linked list
Below is the implementation of the above operation:
C
// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
// If the new node is the
// only node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to delete an element
// at a specified index in the list
void deleteAtIndex()
{
// Stores the index at which
// the element is to be deleted
int pos, i = 1;
struct node *temp, *position;
temp = last->next;
// If list is empty
if (last == NULL)
printf("\nList is empty.\n");
// Else
else {
// Input Data
printf("\nEnter index : ");
scanf("%d", &pos);
// Traverse till the node to
// be deleted is reached
while (i <= pos - 1) {
temp = temp->next;
i++;
}
// After the loop ends, temp
// points at a node just before
// the node to be deleted
// Reassigning links
position = temp->next;
temp->next = position->next;
free(position);
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast();
addatlast();
addatlast();
// Function Call
deleteAtIndex();
// Print the list
viewList();
return 0;
}
Output:

Similar Reads
Menu driven program for all operations on doubly linked list in C
A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. A Doubly Linked List in contains three parts: one is the data part and the other two are the address of the next and previous node in the list. In this article, all the common
5 min read
C Program For Detecting Loop In A Linked List
Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. Solution: Floyd's Cycle-Finding Algorithm Approach: This is the fastest method and has been described below: Traverse linked list using two pointers.Move one pointer(slow_p) by one and anoth
2 min read
C Program For Deleting A Node In A Linked List
We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from
3 min read
C Program For Deleting A Node In A Doubly Linked List
Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read
C# Program For Deleting A Node In A Doubly Linked List
Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read
C Program For Inserting A Node In A Linked List
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. C // A linked list node struct Node { int data; struct Node *next
7 min read
C Program For Deleting A Linked List Node At A Given Position
Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1
3 min read
How to Create a Circular Linked List in C?
The circular linked list is a version of a linked list where the last node does not point to the NULL, but instead, it points back to the first node making a circular loop-like structure. In this article, we will explore how to create a circular linked list in C. What is a Circular Linked List?A cir
3 min read
C Program For Pairwise Swapping Elements Of A Given Linked List
Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp
3 min read
C Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1->3->1->2->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initia
2 min read