This Java program demonstrates how to implement a circular queue using an array. A circular queue is a linear data structure that follows the principle of FIFO (First In First Out), with the last position connected back to the first position to make a circle.
Program Structure
- CircularQueue Class: Contains all the necessary functionalities such as enqueue, dequeue, and display methods.
- Private Members: The class includes members for the queue array, front, rear, and the size of the queue.
Java Code
public class CircularQueue {
private int[] queue;
private int front, rear, size;
public CircularQueue(int size) {
this.size = size;
queue = new int[this.size];
front = -1;
rear = -1;
}
// Enqueue elements to rear
public void enqueue(int element) {
if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) {
System.out.println("Queue is Full");
} else if (front == -1) { // Insert First Element
front = rear = 0;
queue[rear] = element;
} else if (rear == size - 1 && front != 0) {
rear = 0;
queue[rear] = element;
} else {
rear++;
queue[rear] = element;
}
}
// Dequeue element from front
public int dequeue() {
if (front == -1) {
System.out.println("Queue is Empty");
return Integer.MIN_VALUE;
}
int data = queue[front];
queue[front] = 0; // Optional: Clear the slot
if (front == rear) {
front = rear = -1;
} else if (front == size - 1) {
front = 0;
} else {
front++;
}
return data;
}
// Display queue elements
public void displayQueue() {
if (front == -1) {
System.out.println("Queue is Empty");
return;
}
System.out.print("Elements in the Circular Queue are: ");
if (rear >= front) {
for (int i = front; i <= rear; i++)
System.out.print(queue[i] + " ");
} else {
for (int i = front; i < size; i++)
System.out.print(queue[i] + " ");
for (int i = 0; i <= rear; i++)
System.out.print(queue[i] + " ");
}
System.out.println();
}
}
Explanation of How the Program Works
- Initialization: The constructor initializes the queue with a specified size, setting both front and rear to -1 indicating the queue is empty.
- Enqueue Operation: Adds an element to the rear of the queue. If the queue is full, it prints “Queue is Full”. If the queue is empty, it sets both front and rear to 0 and inserts the element. It handles wrapping the rear index to the front if necessary.
- Dequeue Operation: Removes an element from the front of the queue. If the queue is empty, it returns a minimal value. It also adjusts the front index and checks if the queue becomes empty after the operation.
- Display Method: Prints the elements from front to rear, correctly handling the wrap-around of the circular queue.
Key Components:
- CircularQueue Class: This class encapsulates all functionalities of a circular queue including methods to enqueue (add) and dequeue (remove) elements, and to display the contents of the queue.
- Queue Array: The underlying data structure where the queue elements are stored.
- Front and Rear Pointers: These pointers help in determining where to enqueue and dequeue elements, adjusting as the queue wraps around the array.
This setup explains the circular queue’s operations and underlying data structure, making it a comprehensive guide suitable for educational and practical applications in software development.
Conclusion
This circular queue implementation efficiently manages a set size array by using it as a circular structure, thereby optimizing the usage of space by reusing freed positions. It is particularly useful in scenarios where a fixed-size, repetitive queueing of elements is involved, such as buffering data streams.