Implementation Of Circular Queue

 

Here is an implementation of a queue using a circular array in Java. This approach ensures efficient use of space by wrapping around when the end of the array is reached.

public class CircularQueue {
    private int[] queue;
    private int front, rear, size, capacity;

    // Constructor to initialize the queue
    public CircularQueue(int capacity) {
        this.capacity = capacity;
        queue = new int[capacity];
        front = 0;
        rear = -1;
        size = 0;
    }

    // Method to add an element to the queue
    public void enqueue(int data) {
        if (isFull()) {
            System.out.println("Queue is full. Cannot enqueue " + data);
            return;
        }
        rear = (rear + 1) % capacity; // Circular increment
        queue[rear] = data;
        size++;
        System.out.println(data + " enqueued to the queue.");
    }

    // Method to remove an element from the queue
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty. Cannot dequeue.");
            return -1;
        }
        int data = queue[front];
        front = (front + 1) % capacity; // Circular increment
        size--;
        System.out.println(data + " dequeued from the queue.");
        return data;
    }

    // Method to get the front element
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty. No front element.");
            return -1;
        }
        return queue[front];
    }

    // Method to check if the queue is empty
    public boolean isEmpty() {
        return size == 0;
    }

    // Method to check if the queue is full
    public boolean isFull() {
        return size == capacity;
    }

    // Method to get the current size of the queue
    public int getSize() {
        return size;
    }

    // Main method to test the CircularQueue
    public static void main(String[] args) {
        CircularQueue queue = new CircularQueue(5);

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

        System.out.println("Front element: " + queue.peek());
        queue.dequeue();
        queue.enqueue(60);

        System.out.println("Front element: " + queue.peek());
        while (!queue.isEmpty()) {
            queue.dequeue();
        }
    }
}

Explanation:

  1. Circular Behavior: The front and rear pointers wrap around using the modulus operator (%) to ensure circular behavior.
  2. Enqueue: Adds an element to the rear of the queue. If the queue is full, it prints a message.
  3. Dequeue: Removes an element from the front of the queue. If the queue is empty, it prints a message.
  4. Peek: Retrieves the front element without removing it.
  5. Size Management: The size variable keeps track of the number of elements in the queue.

This implementation is efficient and avoids unnecessary shifting of elements, making it ideal for scenarios where a fixed-size queue is required.

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence