Implementing Circular Queue in Java with Arrays

A circular queue is a fixed-size queue where the storage array is treated as a ring. When elements are dequeued from the front, those positions become available again for future insertions — eliminating the wasted space problem of linear array queues. The queue follows the FIFO (First In, First Out) principle: elements are added at the rear and removed from the front.

This post presents a circular queue implementation in Java using a static array-backed design, demonstrating enqueue, dequeue, peek, and display operations through a menu-driven console program.

Java Program: Circular Queue with Arrays

import java.io.*;

// Circular queue using a fixed-size array
class CQueue {
    public int front;    // Index pointing to the front element
    public int rear;     // Index pointing to the rear element
    public int capacity; // Maximum number of elements
    public int queue[];  // Backing array

    public CQueue(int size) {
        capacity = size;
        queue = new int[capacity];
        front = 0;
        rear = -1;  // -1 means no elements yet
    }

    // Returns true when rear has reached the last index
    public boolean isFull() {
        return rear == capacity - 1;
    }

    // Returns true when rear is behind front (no elements between them)
    public boolean isEmpty() {
        return rear < front;
    }

    // Adds an element to the rear of the queue
    public void enqueue(int element) {
        if (isFull()) {
            System.out.println("No Space - Queue is full");
        } else {
            rear++;
            queue[rear] = element;
        }
    }

    // Removes and prints the element at the front of the queue
    public void dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty - nothing to remove");
        } else {
            System.out.println("Removed element: " + queue[front]);
            front++;
        }
    }

    // Displays all elements currently in the queue
    public void display() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
        } else {
            System.out.print("Queue: ");
            for (int i = front; i <= rear; i++) {
                System.out.print(queue[i] + " ");
            }
            System.out.println();
        }
    }

    // Peeks at the front element without removing it
    public void peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
        } else {
            System.out.println("Front element: " + queue[front]);
        }
    }
}

public class CircularQueueDemo2 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter length of queue: ");
        int length = Integer.parseInt(reader.readLine());
        CQueue queue = new CQueue(length);
        int option = 0;
        char exitFlag = ' ';

        do {
            System.out.println();
            System.out.println("--- Circular Queue Operations ---");
            System.out.println("1. Insert an element");
            System.out.println("2. Remove an element");
            System.out.println("3. Display all elements");
            System.out.println("4. Peek at front element");
            System.out.println("5. Exit");
            System.out.print("Select operation: ");
            option = Integer.parseInt(reader.readLine());

            switch (option) {
                case 1:
                    System.out.print("Enter an element: ");
                    int element = Integer.parseInt(reader.readLine());
                    queue.enqueue(element);
                    break;
                case 2:
                    queue.dequeue();
                    break;
                case 3:
                    queue.display();
                    break;
                case 4:
                    queue.peek();
                    break;
                case 5:
                    exitFlag = 'Q';
                    System.out.println("Exiting...");
                    break;
                default:
                    System.out.println("Invalid option");
            }
        } while (exitFlag != 'Q');
    }
}

How the Code Works

  1. CQueue class — Holds a fixed-size array plus front (starting at 0) and rear (starting at -1) pointers.
  2. isFull() — Checks if rear has reached capacity - 1; no more slots available at the back.
  3. isEmpty() — Returns true when rear < front, meaning no elements sit between the two pointers.
  4. enqueue() — Increments rear and stores the element. Prints “No Space” if the queue is full.
  5. dequeue() — Prints and removes the element at front, then increments front.
  6. display() — Iterates from front to rear, printing all current queue elements.

Sample Output

Enter length of queue: 4

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 1
Enter an element: 10

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 1
Enter an element: 20

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 1
Enter an element: 30

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 1
Enter an element: 40

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 1
Enter an element: 50
No Space - Queue is full

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 3
Queue: 10 20 30 40 

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 4
Front element: 10

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 2
Removed element: 10

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 2
Removed element: 20

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 3
Queue: 30 40 

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 2
Removed element: 30

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 2
Removed element: 40

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 2
Queue is empty - nothing to remove

--- Circular Queue Operations ---
1. Insert an element
2. Remove an element
3. Display all elements
4. Peek at front element
5. Exit
Select operation: 5
Exiting...

See Also

Conclusion

Circular queues are a fundamental improvement over simple array queues because they reuse freed space. This implementation demonstrates the core FIFO mechanics using a straightforward array. For applications where slot reuse on wraparound is critical, see the fully circular implementation linked above, which advances both front and rear with modulo arithmetic.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.