00:00

Circular Queue Data Structure

Last updated:

A circular queue is a FIFO queue (First In, First Out) stored in a fixed-size array whose end wraps around to its beginning. It keeps every queue operation at O(1) while reusing all of the array's slots.

What is Circular Queue in Data Structure?

In a circular queue, the array positions are treated as a ring: the position after the last index is index 0. Two indexes, front and rear, move forward around this ring with the modulo operator, for example rear = (rear + 1) % capacity.

This solves the main problem of a simple array queue. There, once rear reaches the last index, no more elements can be added, even if earlier elements have been dequeued and the front of the array is empty. In a circular queue the rear simply wraps around and fills those freed slots.

Because a fixed block of memory can serve an endless stream of elements, circular queues are used for buffers in operating systems, device drivers, networking and audio/video streaming.

A circular queue is also called a ring buffer.

Circular Queue Representation

Circular queue: array positions arranged in a ring with front and rear indexes

Operations on Circular Queue

A circular queue supports these operations, all in O(1) time:

  • Front: return the element at the front without removing it.

  • Rear: return the element at the rear without removing it.

  • enQueue(value): if the queue is full, reject the value; otherwise advance rear by one position (wrapping to 0 after the last index) and store the value there.

  • deQueue(): if the queue is empty, report it; otherwise take the element at front and advance front by one position, wrapping in the same way.

The tricky part is telling "full" from "empty", because in both cases the indexes can meet. The program below uses -1 in both indexes to mark an empty queue, and treats the queue as full when advancing rear would land on front. FAQ Q4 shows two other common techniques.

Example: Circular Queue Implementation in Java

CircularQueue.java
// Java Program to implement a Circular Queue
public class CircularQueue {

  int queue_size;       // size of the queue
  int[] queueArray;     // Array to store the circular queue elements
  int front;            // Index of the front element
  int rear;             // Index of the rear element

  // Constructor
  public CircularQueue(int size) {
    queue_size = size;
    queueArray = new int[queue_size];
    // Initializing front and rear with -1
    front = -1;
    rear = -1;
  }

  // Method to enqueue (add) an element in circular queue
  void enqueue(int item) {
    if (isEmpty()) {
      front = 0;
      rear = 0;
      queueArray[rear] = item;
    } else {
      // Circular increment of rear index
      rear = (rear + 1) % queue_size;
      if (rear == front) {
        System.out.println("Queue is full so Enqueue is not possible.");

        // Reset rear to its previous value
        rear = (rear - 1 + queue_size) % queue_size;
      } else {
        queueArray[rear] = item;
      }
    }
  }

  // Method to Dequeue (remove) an element from the circular queue
  int dequeue() {
    int item = -1; // -1 is returned when the queue is empty

    if (!isEmpty()) {
      item = queueArray[front];
      if (front == rear) {
        // Reset front and rear to -1 to indicate an empty queue
        front = -1;
        rear = -1;
      } else {
        // Circular increment of front index
        front = (front + 1) % queue_size;
      }
    } else {
      System.out.println("Queue is empty so Dequeue is not possible.");
    }

    return item;
  }

  // Method to peek at the front element of the circular queue
  int peek() {
    if (!isEmpty()) {
      return queueArray[front];
    } else {
      System.out.println("Queue is empty");
      return -1; // -1 means "no element"
    }
  }

  // Method to check circular queue is empty
  public boolean isEmpty() {
    return front == -1 && rear == -1;
  }

  public static void main(String[] args) {
    // Create a Circular Queue
    CircularQueue circularQueue = new CircularQueue(5);

    // Adding elements in the circular queue
    circularQueue.enqueue(50);
    circularQueue.enqueue(20);
    circularQueue.enqueue(60);
    circularQueue.enqueue(30);

    // Peek element from the circular queue
    System.out.println("Front element: " + circularQueue.peek());

    // Delete element from the circular queue
    System.out.println("Dequeue: " + circularQueue.dequeue());

    // Peek element after dequeue operation
    System.out.println("Peek after dequeue: " + circularQueue.peek());

    // Adding more elements in the circular queue
    circularQueue.enqueue(90);
    circularQueue.enqueue(66);
    circularQueue.enqueue(72);
  }
}
Output
Front element: 50
Dequeue: 50
Peek after dequeue: 20
Queue is full so Enqueue is not possible.

The queue has capacity 5. After 50, 20, 60 and 30 are added and 50 is removed, the queue holds 20, 60, 30. Adding 90 and 66 fills it (the second of these goes into the slot freed by 50, after rear wraps around), so adding 72 is rejected.

Applications of Circular Queue

  • CPU round-robin scheduling.
  • Keyboard, network and disk I/O buffers.
  • Audio and video streaming buffers.
  • Keeping the most recent N log entries or measurements.
  • Traffic-light controllers that repeat a fixed cycle.

A circular queue makes full use of a fixed amount of memory with constant-time operations. The price is a fixed capacity and slightly trickier index handling.

Advantages of Circular Queue

  • Uses every slot of the array; slots freed by dequeues are reused.
  • Enqueue and dequeue are O(1) and never shift elements.
  • Memory use is fixed and known in advance, which suits embedded and real-time systems.
  • A natural fit for cyclic processing such as round-robin scheduling.

Disadvantages of Circular Queue

  • Harder to get right than a simple queue: the wrap-around and the full/empty test are common sources of bugs.
  • Fixed capacity: when it is full, new elements must be rejected (or, in a ring buffer used for "last N" data, the oldest must be overwritten).
  • Overflow and underflow must still be checked on every enqueue and dequeue.