How to Create a Circular Linked List in C?
Last Updated :
20 Mar, 2024
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 circular linked list is a data structure in which the last node points to the first node, thus forming a circular loop. It is a variation of the traditional singly linked list, where each node points to the next node in the sequence.
Circular Linked ListThe advantage of this kind of linked list is that you can assume any node as the head and traverse the whole linked list from that node.
How to Create a Circular Linked List in C?
In C, each node of the linked list can be represented as a structure with a data field and a pointer to the next node in the sequence. The whole linked list is represented by the pointer to the first node (also called the head).
We can then define the functions to perform the basic operations like insertion, deletion, and traversal. While implementing these functionalities, we have to be extra careful in keeping the check for the end of the linked list as it may easily lead to an infinite loop if missed.
Basic Operations of Circular Linked List
Following are the basic operations on circular linked list:
- Traversal of Circular Linked List - O(n)
- Insertion in Circular Linked List - O(1), if the position is known.
- Deletion in Circular Linked List - O(1), if the position is known.
For our linked list, we will only implement insertion at the end and traversal.
C Program to Create a Circular Linked List
C
// C Program to illustrate how to create a circular linked
// list
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to insert a new node at the end of the list
struct Node* insertAtEnd(struct Node* last, int data)
{
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (last == NULL) {
// If the list is empty, make the new node the only
// node in the list
last = newNode;
newNode->next = last;
}
else {
// Add the new node to the end and update the last
// node's next pointer
newNode->next = last->next;
last->next = newNode;
last = newNode; // Update the last pointer to the
// new node
}
return last; // Return the updated last pointer
}
// Function to display the circular linked list
void display(struct Node* last)
{
if (last == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp
= last->next; // Start from the first node
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != last->next);
printf("\n");
}
int main()
{
struct Node* last
= NULL; // Initialize an empty circular linked list
// Insert nodes at the end
last = insertAtEnd(last, 10);
last = insertAtEnd(last, 20);
last = insertAtEnd(last, 30);
// Display the circular linked list
printf("Circular Linked List: ");
display(last);
return 0;
}
OutputCircular Linked List: 10 20 30
Similar Reads
How to Create a Linked List in C? Write a C program to create a linked list.A linked list is a sequential data structure that stores the data in non-contiguous memory locations unlike array. In a linked list, each element is referred to as a node. Each node in the linked list stores data and a pointer to the next node present in the
5 min read
How to Create a Doubly Linked List in C? A doubly linked list is a type of linked list in which each node contains a pointer to both the next node and the previous node. This allows traversal in both forward and backward directions. Each node in a doubly linked list stores data, a pointer to the next node, and a pointer to the previous nod
4 min read
Program for all operations on Circular Linked List in C 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.
11 min read
Array of Linked Lists in C/C++ An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements that can be accessed randomly using indices of an array. Â They can be used to store the collection of primitive data types such as int, float, double, char,
3 min read
Generic Linked List in C A generic linked list is a type of linked list that allows the storage of different types of data in a single linked list structure, providing more versatility and reusability in various applications. Unlike C++ and Java, C doesn't directly support generics. However, we can write generic code using
5 min read