00:00

Queue Implementation using Array

Last updated:

A queue serves elements in FIFO order (First In, First Out): elements join at the rear (tail) and leave from the front (head).

The simplest way to store a queue is an array with two indexes: front points at the element that will leave next, and rear points at the element that arrived last. This page builds that version in Java.

Operations in Queue

  1. Enqueue: add an element at the rear.

  2. Dequeue: remove the element at the front.

  3. Peek or front: return the front element without removing it.

  4. rear: return the rear element without removing it.

  5. isFull: tell whether no more elements can be added.

  6. isEmpty: tell whether the queue has no elements.

Example: Queue Implementation using Array in Java

How the class works:

  • The queue starts empty with front = 0 and rear = -1; it is empty whenever front > rear.

  • enqueue() increments rear and stores the value there.

  • dequeue() simply increments front. No element is moved, so both operations are O(1).

The limitation of this simple version is that slots before front are never reused: after 100 enqueues the queue reports "full" even if many elements have been dequeued. The circular queue fixes this by wrapping the indexes around the array (see also FAQ Q2).

Class: QueueImplUsingArray

QueueImplUsingArray.java
// A simple (linear) queue of int values stored in an array
class QueueImplUsingArray {
  private final int capacity = 100; // maximum number of enqueues
  private final int[] queueArray;
  private int front;  // index of the front element
  private int rear;   // index of the rear element, front - 1 when empty

  QueueImplUsingArray() {
    queueArray = new int[capacity];
    front = 0;
    rear = -1;
  }

  boolean isEmpty() {
    return front > rear;
  }

  // Adds an element at the rear: O(1)
  void enqueue(int data) {
    if (rear == capacity - 1) {
      System.out.println("Queue is full");
      return;
    }
    queueArray[++rear] = data;
  }

  // Removes the element at the front: O(1), only front moves
  void dequeue() {
    if (isEmpty()) {
      System.out.println("Queue is empty");
      return;
    }
    front++;
  }

  // Prints the elements from front to rear
  void printQueue() {
    if (isEmpty()) {
      System.out.println("Queue is Empty");
      return;
    }
    for (int i = front; i <= rear; i++) {
      System.out.print(queueArray[i] + "  ");
    }
    System.out.println();
  }

  // Shows the front element without removing it
  void front() {
    if (isEmpty()) {
      System.out.println("Queue is Empty");
      return;
    }
    System.out.println("Queue Front Element is: " + queueArray[front]);
  }

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

    // Print queue elements
    queue.printQueue();

    // Insert elements in the queue
    queue.enqueue(20);
    queue.enqueue(30);
    queue.enqueue(40);
    queue.enqueue(50);

    // Print queue elements
    queue.printQueue();

    // Insert element in the queue
    queue.enqueue(60);

    // Print queue elements
    queue.printQueue();

    // Dequeue elements
    queue.dequeue();
    queue.dequeue();

    System.out.print("Queue elements after two node deletions: ");

    // Print queue elements
    queue.printQueue();

    System.out.print("Queue elements after one insertion: ");
    queue.enqueue(60);

    // Print queue elements
    queue.printQueue();

    // Print front of the queue
    queue.front();
  }
}
Output
Queue is Empty
20  30  40  50
20  30  40  50  60
Queue elements after two node deletions: 40  50  60
Queue elements after one insertion: 40  50  60  60
Queue Front Element is: 40