Open In App

CircularArray Implementation of Queue

Last Updated : 24 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

A Circular Queue is a way of implementing a normal queue where the last element of the queue is connected to the first element of the queue forming a circle.

The operations are performed based on the FIFO (First In First Out) principle. It is also called ‘Ring Buffer’. In a normal Queue, we can insert elements until the queue becomes full. However once the queue becomes full, we can not insert the next element even if there is a space in front of the queue.

Operations on Queue

  • getFront: Get the front item from the queue.
  • getRear: Get the last item from the queue.
  • enqueue(value): To insert an element into the circular queue. In a circular queue, the new element is always inserted at the rear position. 
  • dequeue(): To delete an element from the circular queue. In a circular queue, the element is always deleted from the front position.

Simple Array Implementation of Queue

One simple way to implement a queue is by using a simple queue, where elements are added at the rear and removed from the front but it can lead to inefficiency as we need move all elements after front. In this implementation, enqueue() is O(1), but dequeue is O(n).

To read more, Refer Array implementation of queue – Simple

We use a circular array instead of a simple array because a circular array allows both enqueue() and dequeue() in O(1). We move front and rear pointers in circular fashion.

Implement Queue using Circular Array

  1. Initialize an array of size n, where n is the maximum number of elements that the queue can hold.
  2. Initialize three variables (size, capacity, and front.)
  3. Enqueue: To enqueue an element x into the queue, do the following:
    1. Check if size == capacity (queue is full), display “Queue is full”.
    2. If not full: calculate rear = (front + size) % capacity and Insert value at the rear index. Increment size by 1.
  4. Dequeue: To dequeue an element from the queue, do the following:
    1. Check if size == 0 (queue is empty), display “Queue is empty”.
    2. If not empty: retrieve the element at the front index and move front = (front + 1) % capacity. Also, decrement size by 1 and return the removed element.

Illustration of Circular Queue:

Below is the implementation of above approach:

C++
// C++ program for insertion and
// deletion in Circular Queue
#include <iostream>
using namespace std;

class Queue {
private:
    int *arr;        
    int front, size; 
    int capacity;    

public:
  
    // Constructor to initialize the queue
    Queue(int c) {
        arr = new int[c];
        capacity = c;
        size = 0;
        front = 0;
    }

    // Get the front element
    int getFront() {
      
        // Queue is empty
        if (size == 0) 
            return -1; 
        return arr[front];
    }

    // Get the rear element
    int getRear() {
      
        // Queue is empty
        if (size == 0) 
            return -1; 
        int rear = (front + size - 1) % capacity;
        return arr[rear];
    }

    // Insert an element at the rear
    void enqueue(int x) {
      
        // Queue is full
        if (size == capacity) 
            return; 
        int rear = (front + size) % capacity;
        arr[rear] = x;
        size++;
    }

    // Remove an element from the front
    int dequeue() {
      
        // Queue is empty
        if (size == 0) 
            return -1;
        int res = arr[front];
        front = (front + 1) % capacity;
        size--;
        return res;
    }
};

int main() {
    Queue q(4);
    q.enqueue(10);
    cout << q.getFront() << " " << q.getRear() << endl;
    q.enqueue(20);
    cout << q.getFront() << " " << q.getRear() << endl;
    q.enqueue(30);
    cout << q.getFront() << " " << q.getRear() << endl;
    q.enqueue(40);
    cout << q.getFront() << " " << q.getRear() << endl;
    q.dequeue();
    cout << q.getFront() << " " << q.getRear() << endl;
    q.dequeue();
    cout << q.getFront() << " " << q.getRear() << endl;
    q.enqueue(50);
    cout << q.getFront() << " " << q.getRear() << endl;
    return 0;
}
Java Python C# JavaScript

Output
10 10
10 20
10 30
10 40
20 40
30 40
30 50

Complexity Analysis of Circular Queue Operations

Time Complexity:

Operation

Time Complexity

enqueue(x)

O(1)

dequeue()

O(1)

getFront()

O(1)

getRear()

O(1)

Auxiliary Space: O(size), where size is the number of elements in the circular queue.

Related article:



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg