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.
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
← rearExample (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 Features | Linear Queue | Circular Queue |
|---|---|---|
| Space Utilization | Wastes space after deletions | Reuses space efficiently |
| Insert / Delete Cost | O(1) | O(1) |
| Implementation Logic | Simple | Moderate |
| Overflow Condition | When rear reaches end | When full circle completed |
Circular queues are used in:
- CPU scheduling
- Traffic systems
- Real time buffers
- Streaming applications
Core Circular Queue Operations
A circular queue supports:
- enqueue(value) – Insert element at rear
- dequeue() – Remove element from front
- peek() – View front element
- isFull() – Check if queue is full
- isEmpty() – Check if queue is empty
Algorithms for Implementation of Circular Queue using Arrays
If front == 0 AND rear == capacity - 1 OR If front == (rear + 1) % capacity Queue is full
If front == -1 Queue is empty
1. If isFull()
Print "Queue is full"
Return
2. If front == -1
Set front = 0
3. rear = (rear + 1) % capacity
4. queue[rear] = value
1. If isEmpty()
Print "Queue is empty"
Return -1
2. value = queue[front]
3. If front == rear
front = -1
rear = -1
Else
front = (front + 1) % capacity
4. Return value
If isEmpty() return -1 Else return queue[front]
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
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
Time Complexity: O(1)
Space Complexity: O(n)
Note:
In the cases like:
- Empty queue:
If front == -1, the queue is empty — prevent dequeue/peek. - Full queue:
When (rear + 1) % capacity == front, the queue has no free slot. - 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
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