Swift 實現佇列資料結構的程式


佇列是一種遵循 FIFO(先進先出)原則的資料結構。佇列的兩端都是開放的,因此我們可以從一端(稱為後端或尾部)新增新元素,此操作稱為入隊;從另一端(稱為前端或頭部)移除元素,此操作稱為出隊。

雖然 Swift 本身並不支援任何內建的佇列資料結構,但我們仍然可以透過多種方式實現佇列,例如連結串列、結構體、類、陣列等。您可以根據需要選擇任何一種方法來實現佇列資料結構。

佇列支援以下操作:

  • 入隊 (Enqueue) − 用於從佇列的後端或尾部新增元素。在 Swift 中,我們透過 `append()` 方法實現入隊操作。

語法

func Enqueue(_ items: T) {
   item.append(items)
}     

這裡,`append()` 函式向佇列中新增一個新元素。

  • 出隊 (Dequeue) − 用於從佇列的頭部或前端移除元素。在 Swift 中,我們透過 `removeFirst()` 方法實現出隊操作。

語法

func Dequeue() -> T? {
   if item.isEmpty{
      return nil
   } else {
      return item.removeFirst()
   }
}     

這裡,`removeFirst()` 函式用於從佇列中移除元素。

  • 頂部元素 (Top) − 用於獲取佇列的頂部元素。在 Swift 中,我們使用 `first` 屬性實現頂部元素操作。

語法

func Top() -> T? {
   return item.first
}

這裡,`first` 屬性用於查詢棧的頂部元素。

  • 是否為空 (isEmpty) − 用於檢查佇列是否為空。在 Swift 中,我們使用 `isEmpty` 屬性實現是否為空操作。

語法

func IsEmpty() -> Bool {
   return item.isEmpty
}     

這裡,`isEmpty` 屬性用於檢查佇列是否為空。如果返回 true,則表示佇列為空;否則為 false。

  • 大小 (Size) − 用於檢查佇列的大小。在 Swift 中,我們使用 `count` 屬性實現大小操作。

語法

func Size()->Int{
   return item.count
}

這裡,`count` 屬性用於查詢佇列中存在的元素總數。

注意 − 使用陣列實現佇列並不是最有效的方法,因為頻繁的入隊和出隊操作會影響效率。

示例 1

在下面的 Swift 程式中,我們將使用結構體實現佇列資料結構。在這裡,我們定義了一個包含入隊、出隊、頂部元素、大小和是否為空方法的佇列結構體。然後,我們建立一個佇列結構體的例項,使用 `Enqueue()` 函式向佇列中新增一些元素,然後使用 `Dequeue()` 函式按 FIFO 順序移除它們。最後,顯示輸出。

import Foundation
import Glibc

// Implementing stack using structure
struct queue<T> {
   private var item = [T]()
    
   mutating func Enqueue(_ items: T) {
      item.append(items)
   }
    
   mutating func Dequeue() -> T? {
      if item.isEmpty{
         return nil
   } else {
      return item.removeFirst()
   }
}
    
func IsEmpty() -> Bool {
   return item.isEmpty
}
   func Size()->Int{
      return item.count
   }
   func Top() -> T? {
      return item.first
   }
}

var myQueue = queue<Int>()

myQueue.Enqueue(34)
myQueue.Enqueue(42)
myQueue.Enqueue(1)
myQueue.Enqueue(89)
myQueue.Enqueue(32)
myQueue.Enqueue(8)

print("Size of the queue is:", myQueue.Size())
print("Top Element of the queue is:", myQueue.Top()!)

print("Dequeue Elements are:")
while !myQueue.IsEmpty() {
   if let c = myQueue.Dequeue() 
   {
      print(c)
   }
}

輸出

Size of the queue is: 6
Top Element of the queue is: 34
Dequeue Elements are:
34
42
1
89
32
8

示例 2

在下面的 Swift 程式中,我們將使用類實現佇列資料結構。在這裡,我們定義了一個包含入隊、出隊、頂部元素、大小和是否為空方法的佇列類。然後,我們建立一個佇列類的例項,使用 `Enqueue()` 函式向佇列中新增一些元素,然後使用 `Dequeue()` 函式按 FIFO 順序移除它們。最後,顯示輸出。

import Foundation
import Glibc

// Implementing stack using class
class queue<T> {
   private var item = [T]()
    
   func Enqueue(_ items: T) {
      item.append(items)
   }
   
   func Dequeue() -> T? {
      if item.isEmpty{
         return nil
      } else {
         return item.removeFirst()
      }
   }
    
   func IsEmpty() -> Bool {
      return item.isEmpty
   }
   func Size()->Int{
      return item.count
   }
   func Top() -> T? {
      return item.first
   }
}

var myQueue = queue<Character>()

myQueue.Enqueue("Q")
myQueue.Enqueue("U")
myQueue.Enqueue("E")
myQueue.Enqueue("U")
myQueue.Enqueue("E")

print("Size of the queue is:", myQueue.Size())
print("Top Element of the queue is:", myQueue.Top()!)

print("Dequeue Elements are:")
while !myQueue.IsEmpty() {
   if let c = myQueue.Dequeue() 
   {
      print(c)
   }
}

輸出

Size of the queue is: 5
Top Element of the queue is: Q
Dequeue Elements are:
Q
U
E
U
E

結論

這就是我們如何實現佇列資料結構的方法。佇列資料結構可用於排程任務、執行廣度優先搜尋 (BFS) 演算法、非同步操作等。佇列資料結構的靈活性和簡單性使其更適合於有效地管理資料和執行某些特定演算法。

更新於:2023年6月14日

407 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

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