Program In Java To Implement Queue Using Array
The Following Program Reads The Queue Size From User And Displays Menu Containing Queue Operations And Reads Users Choice And Works According To Users Input And Displays Message Accordingly. This Menu Is In Infinite Loop Until User Enters Exit.
import java.io.*; class MyQueue { int[] a; int front, rear; /* Constructors */ MyQueue() //initialize the Queue in a default manner { a = new int[5]; front = rear = -1; } MyQueue(int size) //initialize the Queue in a said manner { a = new int[size]; front = rear = -1; } /* Functions */ void display() { if (!empty()) { System.out.println("\n\nEntered Queue Values Are:: "); for (int j = front; j <= rear; j++) System.out.println("\t\t\t " + a[j]); } else System.out.println("\n\tQueue Is Empty."); } /* Queue Functions */ boolean empty() { return (front == -1 || rear == -1) ? true : false; } void insert(int ele) { if (rear < a.length - 1) { a[++rear] = ele; System.out.println("\nYour Data Has Been Entered Successfully."); } else System.out.println("\nQueue Overflow."); if (front == -1) front++; } String remove() { if (!empty()) return "\nRemoved Element is " + a[front++]; else return "\nQUeue is Empty."; } /* Main Function */ public static void main(String[] arg) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter Your Queue Size:: "); int s = Integer.parseInt(br.readLine()); int m; MyQueue st = new MyQueue(s); while (true) //displaying the menu { System.out.println("\n\n\t\t\tImplementation Of Queue"); System.out.println("\n\tSelect From Below What You Want To Do : "); System.out.println("\n 1. Insert.\n 2. Remove.\n 3. Display.\n 4. Exit."); System.out.print("\nEnter Your Choice : "); m = Integer.parseInt(br.readLine()); switch (m) /*transferring the input to appropriate position*/ { case 1: //Inserting Element In Queue System.out.print("\n\tEnter Integer Element : "); int e = Integer.parseInt(br.readLine()); st.insert(e); break; case 2: //Removing Element From Queue System.out.println(st.remove()); break; case 3: //Displaying The Queue st.display(); break; case 4: //Exiting System.out.println("\n\n\tExited Successfully\n"); System.exit(0); break; default: System.out.println("\n\tInvalid Entry."); System.out.println("\tEnter Proper Input."); break; } } } }
OUTPUT::
Enter Your Queue Size::5
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 1
Enter Integer Element: 12
Your Data Has Been Entered Successfully.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 1
Enter Integer Element: 23
Your Data Has Been Entered Successfully.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 1
Enter Integer Element: 34
Your Data Has Been Entered Successfully.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3
Entered Queue Values Are::12 23 34
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 1
Enter Integer Element: 45
Your Data Has Been Entered Successfully.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 1
Enter Integer Element: 56
Your Data Has Been Entered Successfully.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3
Entered Queue Values Are::12 23 34 45 56
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 1
Enter Integer Element: 67
Queue Overflow.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 2
Removed Element is 12
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 2
Removed Element is 23
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3
Entered Queue Values Are::34 45 56
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 2
Removed Element is 34
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3
Entered Queue Values Are::45 56
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 4
Exited Successfully