← All resources

Write a program to implement an array based queue

Java Program on Queue

This is a Java Program to implement a queue using array. Queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes queue a First-In-First-Out (FIFO) data structure.

Here is the source code of the Java Program to implement a queue using array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

Queue Operations:

push: Adds an item onto the end of the queue.
front: Returns the item at the front of the queue.
pop: Removes the item from the front of the queue.

Overflow State: A queue may be implemented to have a bounded capacity. If the queue is full and does not contain enough space to accept an entity to be pushed, the queue is then considered to be in an overflow state.

Underflow State: The pop operation removes an item from the top of the queue. A pop operation either reveals previously concealed items or results in an empty queue, but, if the queue is empty, it goes into underflow state, which means no items are present in queue to be removed.

Program

import java.io.*;
class Queue
{
    int Q[]; // Array to implement Queue
    int size; // Maximum size of the Queue
    int front; // Index of front element
    int rear; // Index of rear element

    Queue(int cap) // Parameterised Constructor
    {
        size = cap;
        Q = new int[size];
        front = 0;
        rear = 0;
    }

    void push(int v) // Function to insert element in Queue
    {
        if(rear == size) // Condition for Overflow
        {
            System.out.println("OVERFLOW");
        }
        else
        {
            Q[rear] = v; // Storing value in Queue
            rear = rear + 1;   
        }
    }

    int pop() // Function to delete element from Queue
    {
        if(front == 0 && rear == 0) // Condition for Underflow
        {
            System.out.println("UNDERFLOW");
            return -999;
        }
        else
        {
            int val = Q[front]; // Storing the element which will be removed
            front = front + 1;
            if(front == rear) // Condition for emptying the Queue 
            {
                front = 0;
                rear = 0;
            }
            return val;
        }
    }

    void display() // Function for printing elements in the queue
    {
        if(front == 0 && rear == 0)
        {
            System.out.println("The Queue is empty");
        }
        else
        {
            System.out.println("The elements in the queue are : ");
            for(int i=front; i<rear; i++)
            {
                System.out.println(Q[i]);
            }
        }
    }

    public void main()throws IOException
    { 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int ch; 
        do 
        {   //Implementing a menu driven system which will terminate only when the user
            //enters a value less than 1 or greater than 3
            System.out.println("MENU");
            System.out.println("1.PUSH");
            System.out.println("2.POP");
            System.out.println("3.DISPLAY");
            System.out.println("Enter any other number to exit");
            System.out.println("Enter your choice:");
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                int x;
                System.out.print("Enter the number to push: ");
                x = Integer.parseInt(br.readLine());
                push(x);
                break;
                case 2:
                pop();
                break;
                case 3:
                display();
                break;
                default:
                System.out.println("THANK YOU");
            }
        }while(ch>=1&&ch<=3);
    }
}