Java Program to Implement Queue Data Structure Using Arrays

In the previous article, we have seenĀ Java Program to Create an Array and Fill it with Random Integer Values

In this article we are going to see how to Implement Queue data structure using Java programming language.

Java Program to Implement Queue Data Structure Using Arrays

Queue is a linear data structure which stores data in FIFO (First In First Out) order. FIFO principle/order means the element which is inserted first will be deleted first. Means while we insert something that takes takes place at end of the list and while we delete something that takes place at the beginning of the list.

Approach:

  1. Define a class for implementation of the queue data structure.
  2. Create an array of size ‘n‘ to use it as a queue.
  3. Take two variables front and rear both initialized to 0 which indicates that the queue is currently empty.
  4. Front is the index of the first element of the array.
  5. Rear is the index up to which the elements can be stored in the queue.

Following are the implementation of queue operations are as follows:

Enqueue:

  • Addition of an element to the queue.
  • Adding an element will only be if the queue is not full.
  • If rear < n which indicates that the array is not full then store the element at arr[rear] and increment rear by 1.
  • But if rear == n then it is said to be an Overflow condition as the array is full.

Dequeue:

  • Removal of an element from the queue.
  • An element can only be deleted if queue is not empty.
  • If rear > 0 that indicates queue is not empty. Now, element at arr[front] can be deleted.
  • Now shift all the remaining elements left by one position in order delete the element.

Front:

  • Get the front element from the queue i.e. arr[front] if queue is not empty (rear>0).

Display:

  • Print all element of the queue.
  • Check if the queue is not empty.
  • Traverse the queue from front to rear and print all the elements.

Program:

class Queue 
{
    private static int front, rear, capacity;
    private static int queue[];

    Queue(int c)
    {
        front = rear = 0;
        capacity = c;
        queue = new int[capacity];
    }

    // function to insert an element
    // at the rear of the queue
    void queueEnqueue(int data)
    {
        // check queue is full or not
        if (capacity == rear) 
        {
            System.out.print("\nInsert is not possible: Queue is full\n");
            return;
        }

        // insert element at the rear
        else 
        {
            queue[rear] = data;
            rear++;
        }
        return;
    }

    // function to delete an element
    // from the front of the queue
    void queueDequeue()
    {
        // if queue is empty
        if (front == rear) 
        {
            System.out.print("\nDelete is not possible: Queue is empty\n");
            return;
        }

        // shift all the elements from index 2 till rear
        // to the right by one
        else 
        {
            for (int i = 0; i < rear - 1; i++) 
            {
                queue[i] = queue[i + 1];
            }

            // store 0 at rear indicating there's no element
            if (rear < capacity)
                queue[rear] = 0;

            // decrement rear
            rear--;
        }
        return;
    }

    // print queue elements
    void queueDisplay()
    {
        int i;
        if (front == rear) 
        {
            System.out.println("Displaying Queue: Queue is Empty");
            return;
        }

        // traverse front to rear and print elements
        System.out.print("Displaying Queue: ");
        for (i = front; i < rear; i++) 
        {
            System.out.print(+ queue[i] + "<-- " );
        }
        return;
    }

    // print front of queue
    void queueFront()
    {
        if (front == rear) 
        {
            System.out.println("Queue is Empty");
            return;
        }
        System.out.println("\nFront Element is: " + queue[front]);
        return;
    }
}

public class Main {

    // Driver code
    public static void main(String[] args)
    {
        //Create a queue of capacity 4
        Queue q = new Queue(4);

        //print Queue elements
        //as till now we have not inserted any element in queueDisplay
        //so it will return that queue is empty for first time displaying queue
        q.queueDisplay();

        //inserting elements in the queue
        //4 elements inserted, where total cappacity of queue is also 4
        q.queueEnqueue(20);
        q.queueEnqueue(30);
        q.queueEnqueue(40);
        q.queueEnqueue(50);

        //print Queue elements
        //it will 'display the 4 elements' which we had inserted before
        q.queueDisplay();

        //insert element in the queue again
        //It will say 'queue is full' as already we have inserted 4 elements
        //and queue capacity is also 4
        q.queueEnqueue(60);

        //print Queue elements
        q.queueDisplay();
        
        //deleting 3 elements
        // elements will be deleted in FIFO UnsupportedOperationException
        //Means which was inserted first will be deleted first
        //So 3 times we are deleting that means 20,30 and 40 will be deleted from queue
        q.queueDequeue();
        q.queueDequeue();
        q.queueDequeue();
        System.out.println("\nAfter Deleting Three Nodes");

        //print Queue elements
        //After deletion first inserted 3 elements
        //It will print only 50
        q.queueDisplay();

        //print front of the queue
        q.queueFront();
    }
}

Output:

Displaying Queue: Queue is Empty
Displaying Queue: 20<-- 30<-- 40<-- 50<-- 
Insert is not possible: Queue is full
Displaying Queue: 20<-- 30<-- 40<-- 50<-- 
After Deleting Three Nodes
Displaying Queue: 50<-- 
Front Element is: 50

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: