在本文中,我们将看到如何使用java编程语言执行不同的队列操作,如入队、出队、队列前端、队列大小、队列是否为空。我们将使用switch case来实现这个应用程序。
向您展示一些实例实例1suppose 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]
实例-2in the same queue we perform dequeue operation and remove element 2. then theupdated list is - [6, 5, 8, 7, 3, 0]
实例3now we find the front of the queue. the front element is 6.
instance-4的中文翻译为:实例-4suppose 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 的中文翻译为:实例-5now 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<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 : 4enter the element :1234the queue contains:[1 , 2, 3, 4]***menu***1. perform enqueue operation2. perform dequeue operation3. prints the front of the queue4. print the size of the queue5. check if the queue is empty6. terminate the programenter action number (1-6):1enter the element you want to enter in the queue : 5updated list is:[1 , 2, 3, 4, 5]***menu***1. perform enqueue operation2. perform dequeue operation3. prints the front of the queue4. print the size of the queue5. check if the queue is empty6. terminate the programenter action number (1-6):2updated list is:[2, 3, 4, 5]***menu***1. perform enqueue operation2. perform dequeue operation3. prints the front of the queue4. print the size of the queue5. check if the queue is empty6. terminate the programenter action number (1-6):3the front element is 2***menu***1. perform enqueue operation2. perform dequeue operation3. prints the front of the queue4. print the size of the queue5. check if the queue is empty6. terminate the programenter action number (1-6):4the queue size is 4***menu***1. perform enqueue operation2. perform dequeue operation3. prints the front of the queue4. print the size of the queue5. check if the queue is empty6. terminate the programenter action number (1-6):5the queue is not empty***menu***1. perform enqueue operation2. perform dequeue operation3. prints the front of the queue4. print the size of the queue5. check if the queue is empty6. terminate the programenter action number (1-6):6program terminated
在这篇文章中,我们通过使用菜单驱动的方法来探索了如何在java中执行不同的队列操作。
以上就是使用java编写的菜单驱动程序,用于执行队列操作的详细内容。
