基于菜单的Java程序实现队列操作
队列是一种线性数据结构,其中元素以FIFO方式存储。FIFO代表先进先出,这意味着插入的第一个元素将是第一个被访问的元素。在本文中,我们将学习如何使用Java编程语言执行不同的队列操作,例如入队和出队。
Java中的队列
在Java中,Queue接口是Java集合接口的子接口,它提供了队列数据结构的功能。由于它是一个接口,Queue接口需要一个具体的类来实现,最常用的类是PriorityQueue和LinkedList。我们将在程序中使用LinkedList。
Java中队列的操作
我们可以在队列上执行的操作列表如下:
入队操作 - 将元素插入队列的末尾。对于入队操作,使用offer()方法。
出队操作 - 用于从队列中删除元素。对于出队操作,使用poll()方法。
查看队首元素 - 要查看队列中的队首元素,我们使用peek()方法。
检查队列是否为空 - 要检查队列是否为空,我们使用isEmpty()方法。
队列的大小 - 要检查队列的大小或长度,使用size()方法。
示例
下面是一个基于菜单的Java程序,用于执行队列操作:
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!!"); } } } }
在任何Java编译器上运行此程序。输入所需的队列数据和命令来执行与其相关的不同操作。
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
广告