×

Circular Linked List

Circular Linked List

A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes are connected in circular fashion to form a circle. In circular linked list, there is no null node at the end. There are two types of circular linked that can be singly circular linked list or doubly circular linked list.

Circular Linked List

Why we use Circular Linked List

  • In circular linked list, any node can be a starting point. We can traverse the entire list by any point and stop when we revisit the starting point.
  • By circular linked list, we can implement the queue. In queue, we have to maintain two pointers for front and rear but in circular linked list we don’t need to have these pointers.
  • It is used to implement the advanced data structure like Fibonacci Heap

Operations on Circular Linked List: -

  • Insert a node in the linked list:

We can insert a node into the linked list at the front, the end or anywhere in the linked list. The time complexity of these operations is as follows:

  • If we insert the node at the front of linked list; takes O(1)
  • If we insert the node at the end of linked list; takes O(n)
  • If we insert the node anywhere in the linked list; takes O(n)
  • Remove a node from the linked list:

We can remove a node from the linked list from the front, the end or anywhere of the linked list. The time complexity of these operations is as follows:

  • If we remove the node from the front of linked list; takes O(1).
  • If we remove the node from the end of linked list; takes O(n)
  • If we remove the node from anywhere in the linked list; takes O(n)
  • Search a node in the linked list:

Searching a node in the linked list takes O(n) time in the worst case.

Note: - In circular linked list, if we take starting address of the list then the time complexity will be increased for inserting a node at the end of the list and if we take last address of the list then inserting a node at the end of linked list takes constant time e.g. O(1).

Circular Linked List Program in C Language: -

 #include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 void insertAtBeginning(int);
 void insertAtEnd(int);
 void insertAfter(int,int);
 void deleteBeginning();
 void deleteEnd();
 void deleteSpecific(int);
 void display();
 struct Node
 {
    int data;
    struct Node *next;
 }*head = NULL;
 void main()
 {
 int n,pos,item;
 while(1)
 {
 printf(" 1. insert at begin");
 printf("\n 2. insert at specific position");
 printf("\n 3. insert at End");
 printf("\n 4. delete at beginning");
 printf("\n 5. delete specific position");
 printf("\n 6. delete at End");
 printf("\n 7. Traverse the list");
 printf("\n 8.Exit\n");
 printf("Enter the choice:-\n");
 scanf("%d",&n);
 switch(n)
 {
 case 1:
        printf("Enter the item do wanna insert:-\n");
        scanf("%d",&item);
        insertAtBeginning(item);
        break;
 case 2:
      printf("Enter the position where do wanna insert:-\n");
      scanf("%d",&pos);
      printf("Enter the item do wanna insert at position:-\n");
      scanf("%d",&item);
      insertAfter(item,pos);
      break;
 case 3:
        printf("Enter the item do wanna insert at end:-\n");
        scanf("%d",&item);
        insertAtEnd(item);
        break;
 case 4:
        deleteBeginning();
        break;
 case 5:
        printf("Enter the item which do wanna delete :-\n");
        scanf("%d",&item);
        deleteSpecific(item);
        break;
 case 6:
       deleteEnd();
       break;
 case 7:
        printf("your list is:");
        display();
        break;
 case 8:
         exit(0);
 }
 }
 }
 void insertAtBeginning(int value)
 {
     struct Node *newNode;
     newNode = (struct Node*)malloc(sizeof(struct Node));
     newNode -> data = value;
     if(head == NULL)
     {
        head = newNode;
        newNode -> next = head;
     }
     else
     {
        struct Node *temp = head;
        while(temp -> next != head)
           temp = temp -> next;
        newNode -> next = head;
        head = newNode;
        temp -> next = head;
     }
 }
 void insertAtEnd(int value)
 {
    struct Node *newNode;
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode -> data = value;
    if(head == NULL)
    {
       head = newNode;
       newNode -> next = head;
    }
    else
    {
       struct Node *temp = head;
       while(temp -> next != head)
          temp = temp -> next;
       temp -> next = newNode;
       newNode -> next = head;
    }
 }
 void insertAfter(int value, int location)
 {
    struct Node *newNode;
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode -> data = value;
    if(head == NULL)
    {
       head = newNode;
       newNode -> next = head;
    }
    else
    {
       struct Node *temp = head;
       while(temp -> data != location)
       {
          if(temp -> next == head)
          {
             printf("Given node is not found in the list !!!");
          }
          else
          {
             temp = temp -> next;
          }
       }
       newNode -> next = temp -> next;
       temp -> next = newNode;
       printf("\nInsertion success!!!");
    }
 }
 void deleteBeginning()
 {
    if(head == NULL)
       printf("List is Empty!!! Deletion not possible !!!");
    else
    {
       struct Node *temp = head;
       if(temp -> next == head)
       {
          head = NULL;
          free(temp);
       }
       else{
          head = head -> next;
          free(temp);
       }
       printf("\nDeletion success!!!");
    }
 }
 void deleteEnd()
 {
    if(head == NULL)
       printf("List is Empty!!! Deletion not possible !!!");
    else
    {
       struct Node *temp1 = head, *temp2;
       if(temp1 -> next == head)
       {
          head = NULL;
          free(temp1);
       }
       else{
          while(temp1 -> next != head){
             temp2 = temp1;
             temp1 = temp1 -> next;
          }
          temp2 -> next = head;
          free(temp1);
       }
       printf("\nDeletion success!!!");
    }
 }
 void deleteSpecific(int delValue)
 {
    if(head == NULL)
       printf("List is Empty!!! Deletion not possible!!!");
    else
    {
       struct Node *temp1 = head, *temp2;
       while(temp1 -> data != delValue)
       {
          if(temp1 -> next == head)
          {
             printf("\nGiven node is not found in the list !!!");
          }
          else
          {
             temp2 = temp1;
             temp1 = temp1 -> next;
          }
       }
       if(temp1 -> next == head){
          head = NULL;
          free(temp1);
       }
       else{
          if(temp1 == head)
          {
             temp2 = head;
             while(temp2 -> next != head)
                temp2 = temp2 -> next;
             head = head -> next;
             temp2 -> next = head;
             free(temp1);
          }
          else
          {
             if(temp1 -> next == head)
             {
                temp2 -> next = head;
             }
             else
             {
                temp2 -> next = temp1 -> next;
             }
             free(temp1);
          }
       }
       printf("\nDeletion success !!!");
    }
 }
 void display()
 {
 if(head==NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     struct Node *temp=head;
                                     while(temp -> next!=head)
                                     {
                         printf("%d -> ",temp -> data);
                         temp=temp -> next;
                         }
                         printf("%d\n",temp-> data);
 } 

Output

Circular Linked List

Applications of the Circular Linked List

  • It is used to implement round-robin algorithm in operating system
  • It is used in token-ring scheduling in computer networks


Related Topics

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Difference Between Linear and Non Linear Data Structures

Data Structure A data structure is a data object together with the relationships between the instances and the individual elements that compose an instance. These relationships are defined by the operations...

5 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Inorder Successor in Binary Trees

The next node in the Inorder traversal of a binary tree is known as Inorder successor of that particular node. In a Binary Search Tree, the definition of Inorder successor can...

9 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Berkley’s Algorithm

Berkley’s Algorithm is mainly used in clock synchronization system. It is used in distributed systems. To implement this algorithm, we have to think that the network has no accurate time...

4 minutes read.

Preorder Traversal of Binary Trees

In general, Stack, Array, Queue, and other linear data structures only have one way to traverse the data. However, there are numerous ways to traverse through the data in a hierarchical...

3 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

B+ Tree Program in Q language

A B+ tree is just an improvised version of a self-balancing and well-maintained tree in which all the key values that hold valuable information is present at the bottom, which...

9 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.