Open In App

How to Create a Circular Linked List in C?

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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 List

The 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:

  1. Traversal of Circular Linked List – O(n)
  2. Insertion in Circular Linked List – O(1), if the position is known.
  3. 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;
}

Output
Circular Linked List: 10 20 30 





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads