使用Golang實現佇列資料結構


在這個 Golang 程式中,佇列是一種遵循先進先出 (FIFO) 原則的資料結構,元素從隊尾新增,從隊首移除。雖然 Go 沒有內建的佇列資料結構,但可以使用切片、連結串列和其他資料結構來構建一個。我們將使用兩種方法來使用切片和連結串列實現佇列資料結構。

方法 1:使用切片方法

此實現使用切片來儲存佇列中的元素,並實現了佇列資料結構的基本操作:入隊、出隊和判斷佇列是否為空。

演算法

  • 步驟 1 − 建立一個 package main 並宣告 fmt(格式化包)包在程式中,其中 main 生成可執行程式碼,而 fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 在佇列資料結構中建立一個名為 q.item_value 的空切片來儲存元素。

  • 步驟 3 − 在入隊操作中,只需使用 append 函式將元素附加到 q.item_value 切片即可,將其新增到佇列的末尾。

  • 步驟 4 − 在出隊操作中,使用索引從 q.item_value 切片中提取第一個元素並將其放入一個變數中,以從佇列的前面移除一個元素。然後,要刪除第一個元素,請從第二個元素開始切片 q.item_value 切片。返回提取的元素。

  • 步驟 5 − 實現 IsEmpty 函式以確定佇列是否為空,只需確定 q.item_value 的長度是否為 0。

  • 步驟 6 − 此示例演示瞭如何在 Go 中使用基本的佇列資料結構來入隊和出隊元素,以及驗證佇列是否為空。

示例

在此示例中,我們將使用切片來實現佇列資料結構。讓我們看看如何執行它。

package main
import "fmt"

type Queue struct {
   item_value []int
}

func (q *Queue) Enqueue(item int) {
   q.item_value = append(q.item_value, item)  //used to add items
}

func (q *Queue) Dequeue() int {
   item := q.item_value[0]
   q.item_value = q.item_value[1:]  //used to remove items
   return item
}

func (q *Queue) IsEmpty() bool {
   return len(q.item_value) == 0
}

func main() {
   fmt.Println("Enqueue and Dequeue the elements:")
   q := &Queue{}  // create a queue instance which will be used to enqueue and dequeue elements
   q.Enqueue(1)
   q.Enqueue(2)
   q.Enqueue(3)
   for !q.IsEmpty() {
      fmt.Println(q.Dequeue())  //check whether the queue is empty or not
   }
}

輸出

Enqueue and Dequeue the elements:
1
2
3

方法 2:使用連結串列

在此方法中,佇列的元素儲存在連結串列中。連結串列的每個節點都包含一個值和指向下一個節點的指標。頭指標表示佇列的前面,尾指標表示佇列的後面。入隊操作透過修改現有尾節點的 next 指標並將尾指標指向新節點來將新節點新增到連結串列的末尾。出隊操作透過更改頭指標以指向下一個節點來從連結串列中刪除第一個節點。Size 操作返回佇列中元素的總數。

演算法

  • 步驟 1 − 建立一個 package main 並宣告 fmt(格式化包)包在程式中,其中 main 生成可執行程式碼,而 fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 使用一個變數 size 初始化一個佇列資料結構,以跟蹤佇列中元素的數量,以及指向連結串列的頭和尾的指標。

  • 步驟 3 − 將新節點的 next 指標設定為 nil 並使用指定的值建立它。

  • 步驟 4 − 如果佇列為空,則將頭指標和尾指標更新為新節點。

  • 步驟 5 − 否則,更新尾指標和當前尾節點的 next 指標以指向新節點。將 size 變數加 1。

  • 步驟 6 − 如果佇列為空,則返回 0 和 false。否則,將 size 變數減 1,提取第一個節點的值,將頭指標更新為下一個節點,並返回提取的值和 true。

  • 步驟 7 − 使用 size 操作返回 size 變數的值。

  • 步驟 8 − 上述示例演示瞭如何在 Go 中使用連結串列建立佇列資料結構,以及如何使用它來入隊和出隊元素以及獲取佇列的大小。

示例

在此示例中,我們將使用連結串列來實現佇列資料結構。讓我們透過程式碼瞭解一下。

package main
import "fmt"

type Node struct {
   value int
   next  *Node   //use of linked list to implements queue
}

type Queue struct {
   head *Node
   tail *Node
   size int
}

//add the elements in the queue
func (qe *Queue) Enqueue(value int) {
   newNode := &Node{value: value}
   if qe.size == 0 {
      qe.head = newNode
      qe.tail = newNode
   } else {
      qe.tail.next = newNode
      qe.tail = newNode
   }
   qe.size++
}

//remove the elements from queue
func (qe *Queue) Dequeue() (int, bool) {
   if qe.size == 0 {
      return 0, false
   }
   value := qe.head.value
   qe.head = qe.head.next
   qe.size--
   return value, true
}

//return the size of queue
func (qe *Queue) Size() int {
   return qe.size
}

func main() {
   qe := &Queue{}  //create an instance of queue
   qe.Enqueue(1)
   qe.Enqueue(2)
   qe.Enqueue(3)
   fmt.Println("Enqueue and Dequeue the elements:")
   for qe.Size() > 0 {
      value, success := qe.Dequeue()
      if success {
         fmt.Println(value)
      }
   }
}

輸出

Enqueue and Dequeue the elements:
1
2
3

結論

我們使用兩種方法執行了實現佇列資料結構的程式。在第一種方法中,我們使用了切片,在第二個示例中,我們使用了連結串列來執行程式。

更新於: 2023年2月20日

2K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.