×

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

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 minutes read.

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally,...

4 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.