Java 菜单驱动程序执行队列操作

javaserver side programmingprogramming

队列是一种线性数据结构,在 Java 中被视为一个集合,其工作原理是 FIFO(先进先出)。

在本文中,我们将了解如何使用 Java 编程语言执行不同的队列操作,如入队、出队、队列前端、队列大小、队列是否为空。我们将使用 switch case 实现应用程序。

向您展示一些实例 

实例 1

假设我们进入了一个大小为 6 的队列,元素为 [2, 6, 5, 8, 7, 3]。然后我们将执行入队操作并添加元素 0。因此更新后的列表为 -
[2, 6, 5, 8, 7, 3, 0]

实例-2

在同一个队列中,我们执行出队操作并删除元素 2。然后
更新后的列表为 - [6, 5, 8, 7, 3, 0]

实例-3

现在我们找到队列的前面。前面的元素是 6。

实例-4

假设我们创建了一个包含 6 个元素的数组,数组元素为 [2,4,6,2,6,8]。现在我们将打印数组中最小的元素。因此结果将是。
给定数组中存在的最小元素:2

实例-5

现在我们查找队列是否为空。结果是"队列不为空"。

语法

要将元素加入队列,我们​​使用 add() 方法

以下是"add()"的语法 

list.add(s);

要将元素从队列中出队,我们使用 remove() 方法

以下是"remove()"的语法 

list.remove(s);

要查看队列中的前元素,我们使用 peek() 方法

以下是"peek()"的语法 

list.peek();

要检查队列是否为空,我们使用 isEmpty() 方法

以下是"isEmpty()"的语法 

list.isEmpty();

算法

步骤 1 - 要求用户输入所需的队列。

步骤 2 - 显示菜单。

步骤 3 - 要求用户输入他们的选择。

步骤 4 - 使用 switch case 转到选择并执行操作。

步骤 5 - 打印结果。

让我们看一下程序以清楚地理解它。

示例

import java.util.*; public class Main{ public static void main(String args[]){ LinkedList<String> list = new LinkedList<>(); //declare your list Scanner sc = new Scanner(System.in); //create a scanner class object System.out.print("Enter the queue size : "); int nbr = sc.nextInt(); //read the number of element System.out.println("Enter the element : "); sc.nextLine(); do { list.add(sc.nextLine()); nbr--;//decrement the index } while (nbr > 0); //repeat until the index will be 0 System.out.println("The queue contains: "); System.out.println(list);//print your list mainLoop: while (true) { Scanner sc1 = new Scanner(System.in); 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.println("Enter action number (1-6): "); int command = sc.nextInt(); switch(command){ case 1: System.out.print("Enter the element you want to enter in the queue : "); int num = sc.nextInt(); String s = Integer.toString(num); list.add(s); System.out.println("updated list is: "); System.out.println(list); break; case 2: list.remove(); System.out.println("updated list is: "); System.out.println(list); break; case 3: System.out.println("The front element is " + list.peek()); break; case 4: System.out.println("The queue size is " + list.size()); break; case 5: if (list.isEmpty()) { System.out.println("The queue is empty"); } else { System.out.println("The queue is not empty"); } break; case 6: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }

输出

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

在本文中,我们探讨了如何使用菜单驱动方法在 Java 中执行不同的队列操作。


相关文章