基於選單的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

更新於:2024年8月16日

832 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.