Implementation of Circular Queue using Arrays in java

Implementation of Circular Queue using Arrays

Circular Queue using Arrays in Java is an optimized queue implementation that fixes the space wastage problem of a normal (linear) queue. Instead of allowing the rear pointer to reach the end and stop, a circular queue treats the array as a loop, reusing empty slots from the front after deletions.

This makes enqueue and dequeue operations efficient and ideal for fixed size buffers, task schedulers, and systems with fixed memory.

queue using array

What Is a Circular Queue Using Arrays?

Circular queue stores elements sequentially in an array, but unlike a linear queue, when the rear reaches the end of the array, it wraps back to index 0 if space is available.

Example (Before wrap):

front → 10 20 30
               ← rear

Example (After wrap):

If you delete 10 and 20, you can insert at the beginning again:

          rear → 50 60
front → 30    _  _

This wrap around behavior is achieved using:

(nextIndex) = (currentIndex + 1) % capacity

Why Use Circular Queue Instead of Linear Queue?

Algorithm FeaturesLinear QueueCircular Queue
Space UtilizationWastes space after deletionsReuses space efficiently
Insert / Delete CostO(1)O(1)
Implementation LogicSimpleModerate
Overflow ConditionWhen rear reaches endWhen full circle completed

Circular queues are used in:

  1. CPU scheduling
  2. Traffic systems
  3. Real time buffers
  4. Streaming applications

Core Circular Queue Operations

A circular queue supports:

  1. enqueue(value) – Insert element at rear
  2. dequeue() – Remove element from front
  3. peek() – View front element
  4. isFull() – Check if queue is full
  5. isEmpty() – Check if queue is empty
Implementation of Circular Queue

Algorithms for Implementation of Circular Queue using Arrays

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Java Code for Implementation of Circular Queue using Arrays

Run
import java.util.Scanner;

public class CircularQueueArray {

    private int[] queue;
    private int front;
    private int rear;
    private int capacity;

    public CircularQueueArray(int size) {
        capacity = size;
        queue = new int[capacity];
        front = -1;
        rear = -1;
    }

    // Check if queue is full
    public boolean isFull() {
        return (front == 0 && rear == capacity - 1) ||
               (front == (rear + 1) % capacity);
    }

    // Check if queue is empty
    public boolean isEmpty() {
        return front == -1;
    }

    // Insert value (enqueue)
    public void enqueue(int value) {
        if (isFull()) {
            System.out.println("Queue is full. Cannot insert " + value);
            return;
        }
        if (front == -1) {  // first element
            front = 0;
        }
        rear = (rear + 1) % capacity;
        queue[rear] = value;
        System.out.println(value + " inserted");
    }

    // Remove element (dequeue)
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return -1;
        }
        int value = queue[front];

        if (front == rear) {  // only one element
            front = -1;
            rear = -1;
        } else {
            front = (front + 1) % capacity;
        }
        return value;
    }

    // View front element
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return -1;
        }
        return queue[front];
    }

    // Display all elements
    public void display() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return;
        }
        System.out.print("Circular Queue: ");

        int i = front;
        while (true) {
            System.out.print(queue[i] + " ");
            if (i == rear) break;
            i = (i + 1) % capacity;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter queue capacity: ");
        int cap = sc.nextInt();

        CircularQueueArray circularQueue = new CircularQueueArray(cap);

        System.out.println("Operations:");
        System.out.println("1 = Enqueue");
        System.out.println("2 = Dequeue");
        System.out.println("3 = Peek");
        System.out.println("4 = Display");
        System.out.println("5 = Exit");

        while (true) {
            System.out.print("\nEnter choice: ");
            int ch = sc.nextInt();

            switch (ch) {
                case 1:
                    System.out.print("Enter value to insert: ");
                    circularQueue.enqueue(sc.nextInt());
                    break;

                case 2:
                    int removed = circularQueue.dequeue();
                    if (removed != -1) {
                        System.out.println("Removed: " + removed);
                    }
                    break;

                case 3:
                    int frontVal = circularQueue.peek();
                    if (frontVal != -1) {
                        System.out.println("Front element: " + frontVal);
                    }
                    break;

                case 4:
                    circularQueue.display();
                    break;

                case 5:
                    System.out.println("Program terminated.");
                    sc.close();
                    return;

                default:
                    System.out.println("Invalid choice");
            }
        }
    }
}

Input:

Enter queue capacity: 4
1 10
1 20
1 30
4
2
2
1 40
1 50
4

Output:

10 inserted
20 inserted
30 inserted
Circular Queue: 10 20 30
Removed: 10
Removed: 20
40 inserted
50 inserted
Circular Queue: 30 40 50

Note:

In the cases like:

  1. Empty queue:
    If front == -1, the queue is empty — prevent dequeue/peek.
  2. Full queue:
    When (rear + 1) % capacity == front, the queue has no free slot.
  3. Single element removal:
    After removing the only element, reset both pointers to -1.

Frequently Asked Questions

Answer:

It’s a queue implementation where rear wraps to the front using modular arithmetic, allowing reuse of array space.

Answer:

A circular queue is full when (rear + 1) % capacity == front.

Answer:

Both operations take O(1) time.

Answer:

A linear queue wastes space after deletions; a circular queue reuses that space.

Answer:

No, Java does not provide a built in circular queue class: it must be implemented manually.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java