使用Java编写的菜单驱动程序,用于执行队列操作

2023年 8月 28日 65.6k 0

使用Java编写的菜单驱动程序,用于执行队列操作

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

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

向您展示一些实例

实例1

Suppose we have entered a queue of size 6 with elements [2, 6, 5, 8, 7, 3]. Then we will perform the Enqueue operation and add element 0. So the updated list is -
[2, 6, 5, 8, 7, 3, 0]

登录后复制

实例-2

In the same queue we perform Dequeue operation and remove element 2. Then the
updated list is - [6, 5, 8, 7, 3, 0]

登录后复制

实例3

Now we find the front of the queue. The front element is 6.

登录后复制

Instance-4

的中文翻译为:

实例-4

Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the smallest element in an array. And hence result will be.
Smallest element present in given array: 2

登录后复制

Instance-5

的中文翻译为:

实例-5

Now we find if the queue is empty or not. And the result is “The queue is not empty”.

登录后复制

语法

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

以下是“add()”的语法

list.add(s);

登录后复制

为了使队列中的元素出队,我们使用remove()方法

以下是“remove()”的语法

list.remove(s);

登录后复制

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

以下是“peek()”的语法

list.peek();

登录后复制

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

以下是“isEmpty()”的语法:

list.isEmpty();

登录后复制

算法

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

步骤-2 − 显示菜单。

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

Step-4 - 使用开关盒转到选择并执行操作。

第 5 步 - 打印结果。

让我们看看程序就可以清楚地理解它。

示例

import java.util.*;
public class Main{
public static void main(String args[]){
LinkedList 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中执行不同的队列操作。

以上就是使用Java编写的菜单驱动程序,用于执行队列操作的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论