×

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

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.

Bitwise Operators and their Important Tricks

In most of the programs you write today, you deal with data types comprising bytes, such as integer, float, double, etc. Dealing with bytes? It is a quite normal task,...

5 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

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.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 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.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 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.

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.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.