This program implements a Circular Queue using an array in Java. A circular queue connects the last position of the array back to the first position, allowing unused spaces to be reused.
- Enqueue operation
- Dequeue operation
- Peek operation
- Display queue elements
- Reuses empty positions
- Handles full and empty conditions
- Menu-driven program
A Circular Queue follows the FIFO (First In, First Out) principle.
The rear moves forward using the modulo operator:
(rear + 1) % queue.length
When the rear reaches the last position, it moves back to the beginning of the array.
- Create an array for the queue.
- Initialize front and rear to -1.
- Insert elements using Enqueue.
- Move rear circularly using modulo.
- Remove elements using Dequeue.
- Move front circularly after deletion.
- Display elements from front to rear.
- Check whether the queue is full or empty.
- Java
- Arrays
- Queue
- Circular Queue
- FIFO
- Modulo operator
- Methods
- Switch statement
Enqueue: O(1)
Dequeue: O(1)
Peek: O(1)
Display: O(n)
O(n)
10 inserted into queue.
20 inserted into queue.
30 inserted into queue.
Queue elements:
10 20 30
10 removed from queue.
40 inserted into queue.
Front element: 20
Compile:
javac CircularQueue.java
Run:
java CircularQueue
This program helps in understanding circular queues, FIFO operations, array implementation and efficient use of available queue space.
T.Nandhini