Simple Example Program For Queue In Java Using Array and Class

Queue Using Array And Class Java Example Program

This page contains simple Java example programs for Queue Using Array And Class Java Example Program with sample output. This java example program also expain the concepts for clearly.

Go to Program

Queue Wiki Definition:

In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue “dequeue” function. Every time another object or customer enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

Simple Example Program For Queue In Java Using Array and Class

import java.io.*;

class QueueAction {

    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    int items[];
    int i, front = 0, rear = 0, noOfItems, item, count = 0;

    void getdata() {
        try {
            System.out.println("Enter the Limit :");
            noOfItems = Integer.parseInt(is.readLine());
            items = new int[noOfItems];
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    void enqueue() {
        try {
            if (count < noOfItems) {
                System.out.println("Enter Queue Element :");
                item = Integer.parseInt(is.readLine());
                items[rear] = item;
                rear++;
                count++;
            } else {
                System.out.println("Queue Is Full");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    void dequeue() {
        if (count != 0) {
            System.out.println("Deleted Item :" + items[front]);
            front++;
            count--;
        } else {
            System.out.println("Queue IS Empty");
        }
        if (rear == noOfItems) {
            rear = 0;
        }
    }

    void display() {
        int m = 0;
        if (count == 0) {
            System.out.println("Queue IS Empty");
        } else {
            for (i = front; m < count; i++, m++) {
                System.out.println(" " + items[i]);
            }
        }
    }
}

class QueueProgram {

    public static void main(String arg[]) {
        DataInputStream get = new DataInputStream(System.in);
        int choice;
        QueueAction queue = new QueueAction();
        queue.getdata();
        System.out.println("Queue\n\n");
        try {
            do {
                System.out.println("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
                System.out.println("Enter the Choice : ");
                choice = Integer.parseInt(get.readLine());
                switch (choice) {
                    case 1:
                        queue.enqueue();
                        break;
                    case 2:
                        queue.dequeue();
                        break;
                    case 3:
                        queue.display();
                        break;
                }
            } while (choice != 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Sample Output:

Enter the Limit :
4
Queue

1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
1
Enter Queue Element :
45
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
1
Enter Queue Element :
67
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
1
Enter Queue Element :
89
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
1
Enter Queue Element :
567
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
1
Queue Is Full
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
3
 45
 67
 89
 567
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
2
Deleted Item :45
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
2
Deleted Item :67
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
2
Deleted Item :89
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
2
Deleted Item :567
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
2
Queue IS Empty
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice : 
4