How to Create a Circular Linked List in C? Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 ListFollowing 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 Comment More infoAdvertise with us Next Article How to Create a Circular Linked List in C? A arivashiscqxq Follow Improve Article Tags : C Programs C Language Linked Lists C-Pointers C-Structure & Union C Examples +2 More 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 C Program For Writing A Function To Delete A Linked List Algorithm For C:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C // C program to delete a linked list #include<stdio.h> #include<stdlib.h> #includ 2 min read Linked List in C A linked list is a linear data structure where each element (called a node) is connected to the next one using pointers. Unlike array, elements of linked list are stored in random memory locations.In this article, we will learn about the linked list, its types, representation of the linked list in C 7 min read C Program For Flattening A Multilevel Linked List Given a linked list where in addition to the next pointer, each node has a child pointer, which may or may not point to a separate list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in below figure.You are given the head of 5 min read 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 Like