00:00

Queue Implementation using Array

A Queue is a linear data structure to store and manipulate the data elements. A queue follows the concept of "First in, First out" (FIFO), where the first element inserted into the queue is the first one to be deleted from the queue.

A queue is open at both ends which enables insert operations to be performed at one end called REAR or TAIL and delete operations to be performed at another end called FRONT or HEAD.

Operations in Queue

  1. Enqueue: Add a new element to the end of the queue.

  2. Dequeue: Remove an element from the front of the queue.

  3. Peek or front: Retrieve the frontmost element in the queue without deleting it.

  4. rear: Returns the element at the end in the queue without deleting it.

  5. isFull: Checks if the queue is full.

  6. isEmpty: Checks if the queue is empty.

Example: Queue Implementation using Array in Java

Class: QueueImplUsingArray

QueueImplUsingArray.java
class QueueImplUsingArray {
  int front, rear, array_size = 100;
  int[] queueArray;

  // Constructor to initialize the queue
  QueueImplUsingArray() {
    front = 0;
    rear = -1;
    queueArray = new int[array_size];
  }

  // Function to insert an element at the rear of the queue
  void enqueue(int data) {
    // Check if the queue is full
    if (rear == array_size - 1) {
      System.out.println("Queue is full");
      return;
    }

    // Insert element at the rear
    queueArray[++rear] = data;
  }

  // Function to delete an element from the front of the queue
  void dequeue() {
    // If the queue is empty
    if (front > rear) {
      System.out.println("Queue is empty");
      return;
    }

    // Shift all elements from index 1 till rear to the left by one
    for (int i = 0; i < rear; i++) {
      queueArray[i] = queueArray[i + 1];
    }

    // Decrement rear by 1
    rear--;
  }

  // Function to print all elements of queue
  void printQueue() {
    if (front > rear) {
      System.out.println("Queue is Empty");
      return;
    }

    // Traverse front to rear and print elements
    for (int i = front; i <= rear; i++) {
      System.out.print(queueArray[i] + "  ");
    }
    System.out.println(); // Line break
  }

  // Function to print the front of the queue
  void front() {
    if (rear == -1) {
      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