Circular Queue in Java
Circular Queue in Java
Circular Queue in Java is an efficient type of queue that overcomes the space wastage limitation of a normal (linear) queue. In a normal array based queue, once the rear reaches the end, you cannot insert more elements even if there’s vacant space at the front. A circular queue “wraps around” to reuse empty positions in the array, forming a logical circle.
This makes both enqueue (insertion) and dequeue (removal) efficient and ideal for fixed size buffers such as process schedulers, traffic systems, and streaming buffers (ring buffers).
What is a Circular Queue?
Queue is a linear data structure which follows First In First Out (FIFO) principle. But there are some limitations in normal queue. If the rear reaches to the end of the queue then there might be possibility that some vacant spaces are left in the beginning which cannot be utilized.
So , to overcome such limitations , the concept of the circular queue was introduced.
The following are the operations that can be performed on a circular queue:
- Front : It is used to get the front item from the queue.
- Rear : It is used to get the last element from the queue.
- enQueue(value): This function is used to insert the new value in the queue . The new element is always inserted at the rear end.
- deQueue() : This function deletes an element from the queue. The deletion in queue always takes place from the front end.
How Circular Queue works?
- Use a fixed size array.
- Maintain:
- front pointer — index of first element
- rear pointer — index of last element
- Optional size to track count
- For wrap around movement:
nextIndex = (currentIndex + 1) % capacity
This ensures that when you reach the end, you wrap back to the beginning.
Algorithm for Implementation in Circular Queue in Java
For – isFull:
- If (front == 0 && rear == capacity – 1)
- return true
- Else if (front == (rear + 1) % capacity)
- return true
- Else
- return false
For – isEmpty:
- If front == -1
- return true
- Else
- return false
For – enqueue (Insertion)
- If isFull()
- Queue is full
- Else
- If front == -1
- front = 0
- rear = (rear + 1) % capacity
- queue[rear] = value
- If front == -1
For – dequeue (Deletion)
- If isEmpty()
- Queue is empty
- return -1
- value = queue[front]
- If front == rear
- // only one element
- front = rear = -1
- Else:
- front = (front + 1) % capacity
- Return Value
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 in Circular Queue in Java
import java.util.Scanner;
public class CircularQueue {
private int front, rear, capacity;
private int[] queue;
public CircularQueue(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;
}
// Enqueue operation
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");
}
// Dequeue operation
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty (Underflow)");
return -1;
}
int value = queue[front];
// If this was the last element
if (front == rear) {
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 queue
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
System.out.print("CircularQueue: ");
int i = front;
while (true) {
System.out.print(queue[i] + " ");
if (i == rear) break;
i = (i + 1) % capacity;
}
System.out.println();
}
// Main to test CircularQueue
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter queue capacity: ");
int cap = sc.nextInt();
CircularQueue cq = new CircularQueue(cap);
System.out.println("Choose 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 choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value: ");
int val = sc.nextInt();
cq.enqueue(val);
break;
case 2:
int removed = cq.dequeue();
if (removed != -1) {
System.out.println("Removed: " + removed);
}
break;
case 3:
System.out.println("Front: " + cq.peek());
break;
case 4:
cq.display();
break;
case 5:
System.out.println("Program terminated.");
sc.close();
return;
default:
System.out.println("Invalid choice");
}
}
}
}
Input:
Enter queue capacity: 3
1 10
1 20
1 30
1 40
4
2
2
1 50
4
Output:
10 inserted
20 inserted
30 inserted
Queue is full. Cannot insert 40
CircularQueue: 10 20 30
Removed: 10
Removed: 20
50 inserted
CircularQueue: 30 50
Time Complexity: O(1)
Space Complexity: O(n)
Comparison with Linear Queue:
| Feature | Linear Queue | Circular Queue |
|---|---|---|
| Space Usage | Wastes space after deletions | Efficient reuse of space |
| Overflow Condition | Rear reaches end of array | Only when queue is truly full |
| Enqueue/Dequeue Cost | O(1) | O(1) |
| Ease of Implementation | Simple | Moderate |
Frequently Asked Questions
Answer:
A circular queue is a queue where the rear moves to the front after reaching the end, allowing efficient reuse of space.
Answer:
Circular queue avoids space wastage after deletions by wrapping pointers back to the start.
Answer:
Both enqueue and dequeue operations run in O(1) time.
Answer:
A circular queue is full when (rear + 1) % capacity == front.
Answer:
Does Java provide a built in Circular Queue?
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
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
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- 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)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- 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
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java

Login/Signup to comment