Circular Linked List in C

What is circular linked list in C?

A Circular Linked List is a collection of elements connected together with the help of pointers. Such that each node contains a data value and addresses to the next node in the link. Where the last link of the circular Linked list has the address of the first node.

planning

Why circular linked list?

As we know that singly linked list and doubly linked list are already present in there in data structure and algorithm then what is the need of circular linked list?

When we want to access the things in a loop repeatedly. Circular Linked List is ideal so that, whenever we reach the last node we can restart operations again by directly reaching the first node from the last node itself.

Circular Linked List in C

Structure of Circular linked list

Circular linked can be of both singly and doubly types. So they can be defined in a program by using the following set of code.

Singly circular linked list

struct Node {
    int data;
    struct Node *next;
}

Doubly circular linked list

struct node   
{  
    struct node *prev, *next; 
    int data; 
}

Advantages of circular linked list in C

  1. Any node of the list can act as the head as the list does not end.
  2. Access to the first node from the last node is easy and time-saving.
  3. Queues can be easily implemented with a circular linked list.
  4. We can traverse the whole list from any node.

Operation on circular linked list

We can perform the following operations on a circular linked list.

  • Insertion
    • Inserting at Beginning of the list.
    • Inserting at End of the list.
    • Insert at a Specific location in the list.
  • Deletion
    • Deleting from the Beginning of the list.
    • Deleting from End of the list.
    • Deleting a Specific Node from the list

Insertion at the Start of Linked List

Run
#include<stdio.h>
#include<stdlib.h>
// structure for Circular Linked List
struct Node
{
   int data;
   struct Node *next;
};

void insertStart(struct Node** head, int data){
    struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
    newNode->data = data;
    
    // if its the first node being entered
    if(*head == NULL){
        *head = newNode;
        (*head)->next = *head;
        return;
    }
    
    // if LL already as >=1 node
    struct Node* curr = *head;
    
    // traverse till last node in LL
    while(curr->next != *head){
        curr = curr->next;
    }
    // assign LL's last node's next as this new node
    curr->next = newNode;
    
    // assign newNode's next as current head
    newNode->next = *head;
    
    // change head to this new node
    *head = newNode;
}

void display(struct Node* head){
    // if there are no node in LL
    if(head == NULL)
        return;
    
    struct Node* temp = head;
    
    //need to take care of circular structure of LL
    do{
        printf("%d ", temp->data);
        temp = temp->next;
        
    }while(temp!=head);
    printf("\n");
}

int main(){
    
    // first node will be null at creation    
    struct Node* head = NULL;
    
    insertStart(&head,1);
    insertStart(&head,2);
    insertStart(&head,3);
    insertStart(&head,4);
    insertStart(&head,5);
    insertStart(&head,6);

    display(head);
    
    return 0;
}

Output

6 5 4 3 2 1

Insertion at the End of Linked List

Run
#include<stdio.h>
#include<stdlib.h>
// structure for Circular Linked List
struct Node
{
   int data;
   struct Node *next;
};

void insertLast(struct Node** head, int data){
    struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
    newNode->data = data;
    
    // if its the first node being entered
    if(*head == NULL){
        *head = newNode;
        (*head)->next = *head;
        return;
    }
    
    // if LL already as >=1 node
    struct Node* curr = *head;
    
    // traverse till last node in LL
    while(curr->next != *head){
        curr = curr->next;
    }
    
    // assign LL's current last node's next as this new node
    curr->next = newNode;
    
    // assign this new node's next as current head of LL
    newNode->next = *head;
}

void insertStart(struct Node** head, int data){
    struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
    newNode->data = data;
    
    // if its the first node being entered
    if(*head == NULL){
        *head = newNode;
        (*head)->next = *head;
        return;
    }
    
    // if LL already as >=1 node
    struct Node* curr = *head;
    
    // traverse till last node in LL
    while(curr->next != *head){
        curr = curr->next;
    }
    curr->next = newNode;
    newNode->next = *head;
    *head = newNode;
}

void display(struct Node* head){
    // if there are no node in LL
    if(head == NULL)
        return;
    
    struct Node* temp = head;
    
    //need to take care of circular structure of LL
    do{
        printf("%d ", temp->data);
        temp = temp->next;
        
    }while(temp!=head);
    printf("\n");
}

int main(){
    
    // first node will be null at creation    
    struct Node* head = NULL;
    
    insertStart(&head,2);
    insertStart(&head,1);

    display(head);
    
    insertLast(&head, 30);
    insertLast(&head, 40);
    
    display(head);
    
    return 0;
}

Output

1 2 
1 2 30 40

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java