首頁  >  文章  >  Java  >  使用Java編寫的選單驅動程序,用於執行佇列操作

使用Java編寫的選單驅動程序,用於執行佇列操作

PHPz
PHPz轉載
2023-08-27 12:49:03734瀏覽

使用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<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 : 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編寫的選單驅動程序,用於執行佇列操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除