Queue Using Array And Class Example Java Program


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.

Queue Using Array And Class Example Program

import java.io.*;

class QueueMain {

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

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

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

    void dequeue() {
        if (count != 0) {
            System.out.println("Deleted Item :" + items[front]);
            front++;
            count--;
        } else {
            System.out.println("Queue Is Empty");
        }
        if (rear == itemnum) {
            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 instr = new DataInputStream(System.in);
        int choice;
        QueueMain que = new QueueMain();
        que.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(instr.readLine());
                switch (choice) {
                    case 1:
                        que.enqueue();
                        break;
                    case 2:
                        que.dequeue();
                        break;
                    case 3:
                        que.display();
                        break;
                }
            } while (choice != 4);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Sample Output

Output is:
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