使用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
结论
我们使用两种方法执行了实现队列数据结构的程序。在第一种方法中,我们使用了切片,在第二个示例中,我们使用了链表来执行程序。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP