Mastering Queues in Java: A Step-by-Step Implementation (Array Based)

Mastering Queues in Java

Mastering Queues in Java: A Step-by-Step Implementation (Array Based)

In this tutorial, we’ll implement an Array-Based Queue in Java. The code includes detailed comments to explain what each part does.


Queue Class

// Queue class manages all operations using an array
public class Queue {
    private int[] queueArray;  // Array to hold queue elements
    private int front;         // Index of the front element
    private int rear;          // Index of the rear element
    private int capacity;      // Maximum size of the queue
    private int currentSize;   // Current number of elements

    // Constructor initializes the queue with given capacity
    public Queue(int capacity) {
        this.capacity = capacity;
        this.queueArray = new int[capacity];
        this.front = 0;
        this.rear = -1;
        this.currentSize = 0;
    }

    // Check if the queue is empty
    public boolean isEmpty() {
        return this.currentSize == 0;
    }

    // Check if the queue is full
    public boolean isFull() {
        return this.currentSize == this.capacity;
    }

    // Enqueue: Add an element to the rear
    public void enqueue(int data) {
        if (isFull()) {
            System.out.println("Queue Overflow: Cannot enqueue " + data);
        } else {
            this.rear = (this.rear + 1) % this.capacity;  // Circular increment
            this.queueArray[this.rear] = data;            // Add element
            this.currentSize++;
        }
    }

    // Dequeue: Remove and return the front element
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue Underflow: Cannot dequeue from empty queue");
            return -1;  // Or throw exception in real scenarios
        } else {
            int removed = this.queueArray[this.front];    // Get front element
            this.front = (this.front + 1) % this.capacity; // Circular increment
            this.currentSize--;
            return removed;
        }
    }

    // Peek at the front element without removing it
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty: No element to peek");
            return -1;
        } else {
            return this.queueArray[this.front];  // Return front element
        }
    }

    // Print the Queue
    public void printQueue() {
        if (isEmpty()) {
            System.out.println("Queue is Empty");
        } else {
            System.out.print("Queue (front to rear): ");
            int i = this.front;
            for (int count = 0; count < this.currentSize; count++) {  // Traverse from front to rear
                System.out.print(this.queueArray[i] + " ");
                i = (i + 1) % this.capacity;  // Circular traversal
            }
            System.out.println();
        }
    }
}

Main Class – Testing the Queue

// Main class to test Queue operations
public class Main {
    public static void main(String[] args) {
        Queue myQueue = new Queue(5);  // Create queue with capacity 5

        // Enqueue elements into the queue
        myQueue.enqueue(10);
        myQueue.enqueue(20);
        myQueue.enqueue(30);
        myQueue.printQueue();  // Output: Queue (front to rear): 10 20 30

        // Enqueue one more element
        myQueue.enqueue(40);
        myQueue.printQueue();  // Output: Queue (front to rear): 10 20 30 40

        // Peek at front
        System.out.println("Front element: " + myQueue.peek());  // Output: Front element: 10

        // Dequeue elements
        myQueue.dequeue();
        myQueue.printQueue();  // Output: Queue (front to rear): 20 30 40

        myQueue.dequeue();
        myQueue.printQueue();  // Output: Queue (front to rear): 30 40

        // Try to dequeue from near empty
        myQueue.dequeue();
        myQueue.printQueue();  // Output: Queue (front to rear): 40

        // Dequeue last element
        myQueue.dequeue();
        myQueue.printQueue();  // Output: Queue is Empty
    }
}

Final Output


Queue (front to rear): 10 20 30 
Queue (front to rear): 10 20 30 40 
Front element: 10
Queue (front to rear): 20 30 40 
Queue (front to rear): 30 40 
Queue (front to rear): 40 
Queue is Empty

Conclusion

With comments explaining each step, it’s easier to understand how an Array-Based Queue works internally. By practicing enqueues, dequeues, and peeks, you’ll strengthen your foundation in Data Structures, which is essential for advanced topics like breadth-first search, task scheduling, and buffering.

Comments

Popular posts from this blog

Software Engineering Explained: Definition, Process Models, and Challenges

Java Basics – What, Why, and the Real Story

Beginner's Guide to the World Wide Web: Websites, Domains, Networks & More!