
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Menu Driven Program to Perform Queue Operations in Java
A queue is a linear data structure where elements are stored in the FIFO manner. Here, FIFO stands for First In First Out which means the first element inserted would be the first element to be accessed. In this article we will see how to perform different queue operations like Enqueue and Dequeue using Java programming language.
Queue in Java
In Java, the Queue interface, a sub interface of Java Collection Interface, provides the features of queue data structure. Since it is an interface, the Queue interface needs a concrete class for the implementation and the most common classes are the PriorityQueue and LinkedList. We will use LinkedList in our program.
Operation on Queue in Java
List of operations that we can perform on queue is shown below ?
Enqueue operation ? Inserting elements into the queue at the end. For enqueue operation, use offer() method.
Dequeue operation ? It is used to remove elements from the queue. For dequeue operation, use poll() method.
View front element ? To view the front element in the queue we use peek() method.
Check if queue is empty ? To check if queue is empty or not we use isEmpty() method.
Size of queue ? To check size or length of queue, use size() method.
Example
Following is the menu driven Java program to perform queue operation ?
import java.util.*; public class Main { public static void main(String args[]) { Queue<String> queue = new LinkedList<>(); Scanner sc = new Scanner(System.in); // to get queue size System.out.print("Enter the queue size: "); int nbr = sc.nextInt(); sc.nextLine(); // to accept queue elements from user System.out.println("Enter the elements: "); for (int i = 0; i < nbr; i++) { queue.offer(sc.nextLine()); } // printing elements of the queue System.out.println("The queue contains: "); System.out.println(queue); // options while (true) { System.out.println("\n***Menu***"); System.out.println("1. Perform Enqueue operation"); System.out.println("2. Perform Dequeue operation"); System.out.println("3. Prints the front of the queue"); System.out.println("4. Print the size of the queue"); System.out.println("5. Check if the queue is empty"); System.out.println("6. Terminate the program"); System.out.print("Enter action number (1-6): "); int command = sc.nextInt(); sc.nextLine(); // switch statement for different operations switch (command) { case 1: System.out.print("Enter the element you want to insert in the queue: "); queue.offer(sc.nextLine()); System.out.println("Updated queue is: "); System.out.println(queue); break; case 2: if (!queue.isEmpty()) { queue.poll(); System.out.println("Updated queue is: "); System.out.println(queue); } else { System.out.println("The queue is empty, cannot dequeue."); } break; case 3: System.out.println("The front element is: " + queue.peek()); break; case 4: System.out.println("The queue size is: " + queue.size()); break; case 5: System.out.println("The queue is " + (queue.isEmpty() ? "empty" : "not empty")); break; case 6: System.out.println("Program terminated"); return; default: System.out.println("Wrong choice!!"); } } } }
Run this program on any Java compiler. Enter the required queue data and command to perform different operations associated with it.
Enter the queue size : 4 Enter the element : 1 2 3 4 The queue contains: [1 , 2, 3, 4] ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 1 Enter the element you want to enter in the queue : 5 updated list is: [1 , 2, 3, 4, 5] ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 2 updated list is: [2, 3, 4, 5] ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 3 The front element is 2 ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 4 The queue size is 4 ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 5 The queue is not empty ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 6 Program terminated