Write a program to implement a queue using Array.

class Queue {
    int front, rear, size;
    int capacity;
    int array[];

    public Queue(int capacity) {
        this.capacity = capacity;
        front = this.size = 0;
        rear = capacity - 1;
        array = new int[this.capacity];
    }

    // Queue is full when size becomes equal to the capacity
    boolean isFull(Queue queue) {
        return (queue.size == queue.capacity);
    }

    // Queue is empty when size is 0
    boolean isEmpty(Queue queue) {
        return (queue.size == 0);
    }

    // Method to add an item to the queue.
    // It changes rear and size
    void enqueue(int item) {
        if (isFull(this)) {
            return;
        }
        this.rear = (this.rear + 1) % this.capacity;
        this.array[this.rear] = item;
        this.size = this.size + 1;
        System.out.println(item + " enqueued to queue");
    }

    // Method to remove an item from queue.
    // It changes front and size
    int dequeue() {
        if (isEmpty(this)) {
            return Integer.MIN_VALUE;
        }
        int item = this.array[this.front];
        this.front = (this.front + 1) % this.capacity;
        this.size = this.size - 1;
        return item;
    }

    // Method to get front of queue
    int front() {
        if (isEmpty(this)) {
            return Integer.MIN_VALUE;
        }
        return this.array[this.front];
    }

    // Method to get rear of queue
    int rear() {
        if (isEmpty(this)) {
            return Integer.MIN_VALUE;
        }
        return this.array[this.rear];
    }

    public static void main(String[] args) {
        Queue queue = new Queue(1000);

        queue.enqueue(10);
        queue.enqueue(20);
        queue.enqueue(30);
        queue.enqueue(40);

        System.out.println(queue.dequeue() + " dequeued from queue\n");

        System.out.println("Front item is " + queue.front());
        System.out.println("Rear item is " + queue.rear());
    }
}
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue

Front item is 20
Rear item is 40

This program creates a Queue class that has a fixed capacity, and uses an array to store the elements in the queue. The class has several methods that allow adding elements to the queue, removing elements from the queue, and checking the front and rear elements of the queue.

The enqueue() method adds an element to the queue by incrementing the rear pointer and adding the element at the rear of the queue. The dequeue() method removes an element from the queue by incrementing the front pointer and returning the element at the front of the queue. The front() and rear() methods return the front and rear elements of the queue respectively.

In the main method, it creates an instance of the Queue class with a capacity of 1000 and adds several elements to the queue using the enqueue() method. Then it removes an element from the queue using the dequeue() method and prints the result. Finally, it prints the front and rear elements of the queue using the front() and rear() methods.

It’s worth noting that this implementation uses a circular array, which means that when the rear reaches the end of the array, it wraps around to the beginning, allowing for efficient use of the array space. Also, the methods isFull() and isEmpty() are used to check if the queue is full or empty, respectively.

Related Article : Stack and Queue in Data Structure: Complete Guide

Leave a Reply

Your email address will not be published. Required fields are marked *