0

0

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

PHPz

PHPz

发布时间:2023-08-27 12:49:03

|

757人浏览过

|

来源于tutorialspoint

转载

使用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()”的语法 

立即学习Java免费学习笔记(深入)”;

list.add(s);

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

以下是“remove()”的语法

list.remove(s);

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

以下是“peek()”的语法

list.peek();

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

易语言学习手册 十天学会易语言图解教程  pdf版
易语言学习手册 十天学会易语言图解教程 pdf版

十天学会易语言图解教程用图解的方式对易语言的使用方法和操作技巧作了生动、系统的讲解。需要的朋友们可以下载看看吧!全书分十章,分十天讲完。 第一章是介绍易语言的安装,以及运行后的界面。同时介绍一个非常简单的小程序,以帮助用户入门学习。最后介绍编程的输入方法,以及一些初学者会遇到的常见问题。第二章将接触一些具体的问题,如怎样编写一个1+2等于几的程序,并了解变量的概念,变量的有效范围,数据类型等知识。其后,您将跟着本书,编写一个自己的MP3播放器,认识窗口、按钮、编辑框三个常用组件。以认识命令及事件子程序。第

下载

以下是“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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
excel制作动态图表教程
excel制作动态图表教程

本专题整合了excel制作动态图表相关教程,阅读专题下面的文章了解更多详细教程。

20

2025.12.29

freeok看剧入口合集
freeok看剧入口合集

本专题整合了freeok看剧入口网址,阅读下面的文章了解更多网址。

65

2025.12.29

俄罗斯搜索引擎Yandex最新官方入口网址
俄罗斯搜索引擎Yandex最新官方入口网址

Yandex官方入口网址是https://yandex.com;用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

197

2025.12.29

python中def的用法大全
python中def的用法大全

def关键字用于在Python中定义函数。其基本语法包括函数名、参数列表、文档字符串和返回值。使用def可以定义无参数、单参数、多参数、默认参数和可变参数的函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

python改成中文版教程大全
python改成中文版教程大全

Python界面可通过以下方法改为中文版:修改系统语言环境:更改系统语言为“中文(简体)”。使用 IDE 修改:在 PyCharm 等 IDE 中更改语言设置为“中文”。使用 IDLE 修改:在 IDLE 中修改语言为“Chinese”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

C++的Top K问题怎么解决
C++的Top K问题怎么解决

TopK问题可通过优先队列、partial_sort和nth_element解决:优先队列维护大小为K的堆,适合流式数据;partial_sort对前K个元素排序,适用于需有序结果且K较小的场景;nth_element基于快速选择,平均时间复杂度O(n),效率最高但不保证前K内部有序。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

12

2025.12.29

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

134

2025.12.29

抖音网页版入口在哪(最新版)
抖音网页版入口在哪(最新版)

抖音网页版可通过官网https://www.douyin.com进入,打开浏览器输入网址后,可选择扫码或账号登录,登录后同步移动端数据,未登录仅可浏览部分推荐内容。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

63

2025.12.29

快手直播回放在哪看教程
快手直播回放在哪看教程

快手直播回放需主播开启功能才可观看,主要通过三种路径查看:一是从“我”主页进入“关注”标签再进主播主页的“直播”分类;二是通过“历史记录”中的“直播”标签页找回;三是进入“个人信息查阅与下载”里的“直播回放”选项。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

18

2025.12.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Rust 教程
Rust 教程

共28课时 | 3.9万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号