Launch your tech mastery with us—your coding journey starts now!
Course Content
Data Structure

Circular Queue

A circular queue is the extended version of a regular queue where the last element is connected to the first element. Thus forming a circle-like structure.

The circular queue solves the major limitation of the normal queue. In a normal queue, after a bit of insertion and deletion, there will be non-usable empty space.

Circular queue representation

Circular Queue data structure illustration showing elements A, B, C, D arranged in a circle with arrows indicating enqueue at rear and dequeue at front positions.

Code Implementation

This implementation uses the modulo operator % to wrap indices around .

JavaPythonCC++

class CircularQueue {
    private int maxSize;
    private int[] queueArray;
    private int front;
    private int rear;
    private int currentSize;

    public CircularQueue(int size) {
        this.maxSize = size;
        this.queueArray = new int[maxSize];
        this.front = 0;
        this.rear = -1;
        this.currentSize = 0;
    }

    public void enqueue(int value) {
        if (isFull()) {
            System.out.println("Queue is full");
            return;
        }
        rear = (rear + 1) % maxSize; // Circular increment
        queueArray[rear] = value;
        currentSize++;
        System.out.println("Enqueued: " + value);
    }

    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return -1;
        }
        int value = queueArray[front];
        front = (front + 1) % maxSize; // Circular increment
        currentSize--;
        System.out.println("Dequeued: " + value);
        return value;
    }

    public boolean isEmpty() { return currentSize == 0; }
    public boolean isFull() { return currentSize == maxSize; }
}

class CircularQueue:

    def __init__(self, size):
        self.maxSize = size
        self.queueArray = [0] * size
        self.front = 0
        self.rear = -1
        self.currentSize = 0


    def enqueue(self, value):

        if self.isFull():
            print("Queue is full")
            return

        self.rear = (self.rear + 1) % self.maxSize  # Circular increment
        self.queueArray[self.rear] = value
        self.currentSize += 1

        print("Enqueued:", value)


    def dequeue(self):

        if self.isEmpty():
            print("Queue is empty")
            return -1

        value = self.queueArray[self.front]

        self.front = (self.front + 1) % self.maxSize  # Circular increment
        self.currentSize -= 1

        print("Dequeued:", value)
        return value


    def isEmpty(self):
        return self.currentSize == 0


    def isFull(self):
        return self.currentSize == self.maxSize

#include <stdio.h>

#define MAX 100

struct CircularQueue {
    int queueArray[MAX];
    int front;
    int rear;
    int currentSize;
    int maxSize;
};


void initQueue(struct CircularQueue* q, int size) {
    q->maxSize = size;
    q->front = 0;
    q->rear = -1;
    q->currentSize = 0;
}


void enqueue(struct CircularQueue* q, int value) {

    if (q->currentSize == q->maxSize) {
        printf("Queue is full\n");
        return;
    }

    q->rear = (q->rear + 1) % q->maxSize;
    q->queueArray[q->rear] = value;
    q->currentSize++;

    printf("Enqueued: %d\n", value);
}


int dequeue(struct CircularQueue* q) {

    if (q->currentSize == 0) {
        printf("Queue is empty\n");
        return -1;
    }

    int value = q->queueArray[q->front];

    q->front = (q->front + 1) % q->maxSize;
    q->currentSize--;

    printf("Dequeued: %d\n", value);
    return value;
}


int isEmpty(struct CircularQueue* q) {
    return q->currentSize == 0;
}


int isFull(struct CircularQueue* q) {
    return q->currentSize == q->maxSize;
}

#include <iostream>
using namespace std;

class CircularQueue {

private:
    int maxSize;
    int* queueArray;
    int front;
    int rear;
    int currentSize;

public:

    CircularQueue(int size) {
        this->maxSize = size;
        this->queueArray = new int[maxSize];
        this->front = 0;
        this->rear = -1;
        this->currentSize = 0;
    }


    void enqueue(int value) {

        if (isFull()) {
            cout << "Queue is full" << endl;
            return;
        }

        rear = (rear + 1) % maxSize; // Circular increment
        queueArray[rear] = value;
        currentSize++;

        cout << "Enqueued: " << value << endl;
    }


    int dequeue() {

        if (isEmpty()) {
            cout << "Queue is empty" << endl;
            return -1;
        }

        int value = queueArray[front];

        front = (front + 1) % maxSize; // Circular increment
        currentSize--;

        cout << "Dequeued: " << value << endl;
        return value;
    }


    bool isEmpty() { return currentSize == 0; }
    bool isFull() { return currentSize == maxSize; }
};