×

Circular Queue

Circular Queue

Circular Queue is special type queue, which follows First in First Out (FIFO) rule and as well as instead of ending queue at the last position, it starts again from the first position after the last position and behaves like circular linear data structure.

We can say it is extension of the queue data structure such that last element of the queue is associated with the first element of the queue and it is also referred as Circular Buffer. If we talk about normal queue, no additional element can be added if queue if full. Also, we can’t add the element in the queue if there is space left in front of queue, so to overcome this problem Circular Queue comes to the picture, so we can use circular queue to solve this problem with efficient manner.

Circular Queue

Operations on Circular Queue

Enqueue Operation in Circular Queue:

  • Firstly, we have to whether the queue is full or not
  • For adding the first element in the queue, set front to 0
  • Increase rear by 1 circularly and add the new element at the position which is pointed by rear

Dequeue Operation in Circular Queue:

  • Firstly, we have to check whether the queue is empty or not.
  • Return the element which is pointed by front.
  • Increase front by 1
  • In case of last element of the queue, set values of front and rear to -1

Implementation of Circular Queue

The number of ways for implementing the circular queue using:

  • Array
  • Linked List

Circular Queue Implementation in C language

Using Array: -

# include < stdio.h >
   # define  len 7
  int queue[len];
   int front = -1, rear = -1;
 void enQueue(int item) // For add an element to the queue
  {
 if ((front == rear+1) || (front == 0 && rear == len-1)) // For checking whether the queue is full or not
    {
     printf("\nQueue is full\n");
     return;
    }
    else
   {
   if(front  ==  -1)
    {
   front = 0;
    }          
 rear = (rear+1) % len;
  queue[rear] = item;
 }          
  }
 void deQueue()   // For remove an element from the queue
  {
 if( front == -1 && rear == -1)     // For checking whether the queue is empty or not
   {
  printf("\nQueue is Empty\n");
  return;
  }
 else
  {
  printf("\n Deleted Element is %d" , queue[front]);
        if(front == rear)
    {
 front = - 1;
  rear = - 1;
   }
   else
  front = (front+1) % len;
        }
   }
    void traverse() //For print the elements of the queue
   {
  int i = front;
  printf("\nFront: ");
  while(i != rear)
  {          
    printf("%d ",queue[i]);
    i = (i+1) % len;
      }
     printf("%d",queue[rear]);
   printf(" :Rear");
       }
 int main()    // Driver code
 {
        enQueue(23);
     enQueue(33);
     enQueue(43);
       enQueue(53);
      enQueue(63);
  enQueue(73);
 enQueue(83);
     traverse();
 deQueue();
      traverse();
 enQueue(55);
     traverse();
   return 0;
 } 

Output

Circular Queue

Using LinkedList: -

#include<stdio.h>
 #include<stdlib.h>
 struct node // For declaration new node
 {
             int data;
             struct node* next;
 };
 struct node *front = NULL;
 struct node *rear = NULL;
 void enQueue(int d) // For add an element to the queue
 {
             struct node * n;
             n = (struct node*)malloc(sizeof(struct node));
             n->data = d;
             n->next = NULL;
             if((front == NULL)&&(rear == NULL))
             {
                         front = rear = n;
                         rear->next = front;
             }
             else
             {
                         rear->next = n;
                         rear = n;
                         n->next = front;
             }
 }
 void deQueue()// For remove an element from the queue
 {
             struct node* t;
             t = front;
             if((front == NULL)&&(rear == NULL)) // For checking whether the queue is empty or not
                         printf("\nQueue is Empty!!");
             else if(front == rear)
 {
                         front = rear = NULL;
                         free(t);
             }
             else
 {
                         front = front->next;
                         rear->next = front;
                         free(t);
             }
 }
 void traverse()//For print the elements of the queue
 {
             struct node* t;
             t = front;
             if((front==NULL)&&(rear==NULL))
                         printf("\nQueue is Empty!!");
             else{
                 printf("Queue elements are:");
                         do{
                                     printf("%d ",t->data);
                                     t = t->next;
                         }while(t != front);
             }
 }
 int main() // Driver code
 {
             int choice,n,i,data;     
             do{
                         printf("\n1 for Insert the Data \n2 for print the Data \n3 for Delete the data \n4 for Exit");
                         printf("\nEnter Your Choice:-");
                         scanf("%d",&choice);
                         switch(choice)
 {
           case 1:
                 printf("\nEnter the number of data: ");
                  scanf("%d",&n);
                  printf("\nEnter your data: ");
                 i=0;
               while(i<n){
             scanf("%d",&data);
            enQueue(data);
             i++;
       }
               break;
               case 2:
                traverse();
                break;
             case 3:
             deQueue();
              break;
      case 4:
         break;
       }
      }
 while(choice!=4);
 return 0;
 } 

Outputs: -

Circular Queue

Circular Queue Implementation in Java language

  public class CircularQueue
 {
 int len = 5;
 int front, rear;
 int queue[] = new int[len];
 CircularQueue()
  {
 front = -1;
 rear = -1;
 }
 int isFull()// For checking whether the queue is full or not
 {
 if (front == 0 && rear == len -1 || front == rear + 1 )
 {
 return 1;
 }
 return 0;
 }
 int isEmpty()// For checking whether the queue is empty or not
 {
 if (front == -1)
 return 1;
 else
 return 0;
 }
 void enQueue(int item) // For add an element to the queue
 {
 if (isFull()==1)
 {
 System.out.println("Queue is full");
 }
 else
 {
 if (front == -1)
 front = 0;
 rear = (rear + 1) % len;
 queue[rear] = item;
 }
 }
 int deQueue()// For remove an element from the queue
 {
 int item;
 if (isEmpty()==1)
 {
 System.out.println("Queue is empty");
 return (-1);
 }
 else
 {
 item = queue[front];
 if (front == rear)
 {
 front =  -1;
 rear =  -1;
 }
 else
 {
 front = (front + 1) % len;
 }
 return (item);
 }
 }
 void traverse()//For print the elements of the queue
 {
 int i;
 if (isEmpty() == 1)
 {
 System.out.println("Empty Queue");
 }
 else
 {
 System.out.print("Front: ");
 for (i = front ; i != rear ; i = (i + 1) % len)
 System.out.print(queue[i] + " ");
 System.out.print(queue[i]);
 System.out.println(" :Rear" );
 }
 }
 public static void main(String[] args)// Driver code
 {
 CircularQueue q = new CircularQueue();
 q.deQueue();
 q.enQueue(1);
 q.enQueue(2);
 q.enQueue(3);
 q.enQueue(4);
 q.enQueue(5);
 q.enQueue(6);
 q.traverse();
 int element = q.deQueue();
 if (element != -1)
 {
 System.out.println("Deleted Element is " + element);
 }
 q.traverse();
 q.enQueue(7);
 q.traverse();
 q.enQueue(8);
 }
 } 

Output: -

Circular Queue

Time Complexity: -

if we talk about time complexity of enqueue and dequeue operation of a Circular Queue is O(1) for array Implementation.

Advantages of Circular Queue

  • No leaks of memory because it doesn’t use dynamic memory.
  • Simple implementation
  • All operations in constant time O(1)

Applications of Circular Queue

  • Memory Management in Operating System
  • CPU Scheduling
  • Buffering Data Streams
  • Trafficking Signal Systems

Related Topics

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Trie data structure

Trie data structure The term “trie” comes from the word “retrieval” which means getting information. The trie data structure is a sorted extension of tree-based data structure. The trie data structure...

5 minutes read.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

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

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Reverse the Singly Linked List in C

Reverse the Singly Linked List in C This article has given a singly linked list and will reverse the linked list by changing the links between nodes. Example:                         Input:  2 -> 4...

3 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

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.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Finding the Maximum Element in a Binary Tree

Implementation // Creating a C++ program to excavate the minimum and maximum in a given binary tree. #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new tree node. class __nod { public: int record; __nod *Lft, *Rt; /*...

4 minutes read.

Bubble Sort in Data Structures

Bubble Sort in C++ The bubble sort algorithm analyses two adjacent elements and swaps them until they are no longer in the desired order. Each iteration moves each member of the array...

4 minutes read.