Give the interface definition for an abstract data type 'Queue'. Queue is to be implemented in an array defined in the interface of the queue.


Q.) Give the interface definition for an abstract data type 'Queue'. Queue is to be implemented in an array defined in the interface of the queue.

Subject: Data Structures

An abstract data type (ADT) Queue is a linear data structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first.

The basic operations associated with a Queue are:

  1. enqueue: Add an element to the end of the queue.
  2. dequeue: Remove an element from the front of the queue.
  3. isFull: Check if the queue is full.
  4. isEmpty: Check if the queue is empty.
  5. front: Get the value of the front of the queue without removing it.
  6. rear: Get the value of the end of the queue without removing it.

Here is an interface definition for a Queue ADT using an array:

public interface QueueADT {
    // Adds an item to the rear of the queue.
    void enqueue(int item) throws QueueFullException;

    // Removes the front item from the queue and returns it.
    int dequeue() throws QueueEmptyException;

    // Returns true if the queue is full, false otherwise.
    boolean isFull();

    // Returns true if the queue is empty, false otherwise.
    boolean isEmpty();

    // Returns the item at the front of the queue without removing it.
    int front() throws QueueEmptyException;

    // Returns the item at the rear of the queue without removing it.
    int rear() throws QueueEmptyException;
}

Now let's define the exceptions that might be thrown by the queue operations:

public class QueueFullException extends RuntimeException {
    public QueueFullException(String message) {
        super(message);
    }
}

public class QueueEmptyException extends RuntimeException {
    public QueueEmptyException(String message) {
        super(message);
    }
}

And here is a simple array-based implementation of the Queue interface:

public class ArrayQueue implements QueueADT {
    private int[] queue;
    private int front;
    private int rear;
    private int size;
    private int capacity;

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

    public void enqueue(int item) throws QueueFullException {
        if (isFull()) {
            throw new QueueFullException("Queue is full");
        }
        this.rear = (this.rear + 1) % this.capacity;
        this.queue[this.rear] = item;
        this.size = this.size + 1;
    }

    public int dequeue() throws QueueEmptyException {
        if (isEmpty()) {
            throw new QueueEmptyException("Queue is empty");
        }
        int item = this.queue[this.front];
        this.front = (this.front + 1) % this.capacity;
        this.size = this.size - 1;
        return item;
    }

    public boolean isFull() {
        return (this.size == this.capacity);
    }

    public boolean isEmpty() {
        return (this.size == 0);
    }

    public int front() throws QueueEmptyException {
        if (isEmpty()) {
            throw new QueueEmptyException("Queue is empty");
        }
        return this.queue[this.front];
    }

    public int rear() throws QueueEmptyException {
        if (isEmpty()) {
            throw new QueueEmptyException("Queue is empty");
        }
        return this.queue[this.rear];
    }
}

In this implementation, the enqueue and dequeue methods wrap around the array using modular arithmetic to avoid having to shift elements. This is a common optimization in circular queue implementations.

Here's a summary of the key points in a table format:

Operation Description Time Complexity Throws Exception
enqueue Adds an item to the end of the queue O(1) QueueFullException if the queue is full
dequeue Removes the front item from the queue and returns it O(1) QueueEmptyException if the queue is empty
isFull Checks if the queue is full O(1) -
isEmpty Checks if the queue is empty O(1) -
front Returns the item at the front of the queue without removing it O(1) QueueEmptyException if the queue is empty
rear Returns the item at the rear of the queue without removing it O(1) QueueEmptyException if the queue is empty

This table provides a quick reference to the operations of the Queue ADT, their descriptions, time complexities, and any exceptions they might throw.