Queue on Arrays in Java

Queue is a linear data structure that follows the FIFO (First In First Out) mechanism.

Queues can be implemented on both arrays and linked lists.

In this post, we will see how queues can be implemented on arrays.

Inserting an element into a queue is termed as push operation.

Deleting an element from the queue is termed as pop operation.

Attempting to push an element when the queue is already full is known as queue overflow.

Attempting to pop an element when the queue is empty is known as queue underflow.

The front data member points to the first element in the queue.

The rear data member points to the last element in the queue.

import java.io.*;
class QueueOnArray{
    int q[];
    int front;
    int rear;
    int size;
    public QueueOnArray(int cap){
        size = cap;
        q = new int[size];
        front = 0;
        rear = -1;
    }
    public void push(int data){
        if(rear + 1 == size)
            System.out.println("Queue overflow!");
        else{
            q[++rear] = data;
            System.out.println(data + " pushed.");
        }
    }
    public void pop(){
        if(rear == -1)
            System.out.println("Queue underflow!");
        else{
            int data = q[front];
            front++;
            System.out.println(data + " popped.");
        }
        if(front > rear){
            front = 0;
            rear = -1;
        }
    }
    public void display(){
        if(rear == -1)
            System.out.println("Queue underflow!");
        else{
            System.out.print("Queue elements: ");
            for(int i = front; i <= rear; i++)
                System.out.print(q[i] + "\t");
            System.out.println();
        }
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Queue size: ");
        int cap = Integer.parseInt(br.readLine());
        QueueOnArray obj = new QueueOnArray(cap);
        while(true){
            System.out.println("1. Push");
            System.out.println("2. Pop");
            System.out.println("3. Display");
            System.out.println("Enter your choice: ");
            int choice = Integer.parseInt(br.readLine());
            switch(choice){
                case 1:
                System.out.print("Data to be pushed: ");
                int d = Integer.parseInt(br.readLine());
                obj.push(d);
                break;
                case 2:
                obj.pop();
                break;
                case 3:
                obj.display();
                break;
                default:
                System.out.println("Bye...");
                return;
            }
        }
    }
}

6 thoughts on “Queue on Arrays in Java

  1. Sir,a program is given which states that:
    WAP in JAVA to accept a string.Count & display the frequency of each character present in the string.The character with multiple frequencies should be displayed only once.
    Sample Input:golden jubilee
    Sample Output:
    Alphabet Frequency
    g——-> 1
    o——-> 1
    l——–> 2
    d——–> 1
    e———> 3
    n———->1
    j———->1
    u———->1
    b———->1
    i———->1

  2. Sir,how to perform dequeue operations using array as well as through linked list using menu driven program?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.