Program In Java To Implement Circular Queue Using Arrays.
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 MyCQueue { int[] a; int front, rear; /* Constructors */ MyCQueue() //initialize the queue in a default manner { a = new int[5]; front = rear = -1; } MyCQueue(int size) //initialize the queue in a said manner { a = new int[size]; front = rear = -1; } /* Queue Functions */ boolean empty() { return (front == -1 || rear == -1) ? true : false; } void insert(int ele) { if (front == (rear + 1) % a.length) System.out.println("\nQueue Overflow."); else { rear = (rear + 1) % a.length; a[rear] = ele; System.out.println("\nYour Data Has Successfully Been Entered."); } if (front == -1) front++; } String remove() { if (!empty()) { String s = "\nRemoved Element :" + a[front]; if (front == rear) front = rear = -1; else front = ((front + 1) % a.length); return s; } else return "\nQueue is Empty."; } /* Functions */ void display() //displaying the Queue { if (!empty()) { int j = front; do { System.out.println(a[j]); j = (j + 1) % a.length; } while (j != rear); System.out.println(a[j]); } else System.out.println("\n\tQueue 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; MyCQueue st = new MyCQueue(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 Successfully Been Entered.
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 Successfully Been Entered.
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 Successfully Been Entered.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3 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 Successfully Been Entered.
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 Successfully Been Entered.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3 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: 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: 23
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3 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: 34
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3 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: 1
Your Data Has Successfully Been Entered.
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: 2
Your Data Has Successfully Been Entered.
Implementation Of Queue
Select From Below What You Want To Do: 1. Insert. 2. Remove. 3. Display. 4. Exit.
Enter Your Choice: 3 45 56 1 2
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