feat(go/deque): support go array deque (#414)

This commit is contained in:
Reanon 2023-03-13 20:16:23 +08:00 committed by GitHub
parent 6924d15f63
commit be0c965886
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 159 additions and 1 deletions

View file

@ -0,0 +1,115 @@
// File: array_deque.go
// Created Time: 2023-03-13
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import "fmt"
/* 基于环形数组实现的双向队列 */
type arrayDeque struct {
nums []int // 用于存储双向队列元素的数组
front int // 队首指针,指向队首元素
queSize int // 双向队列长度
queCapacity int // 队列容量(即最大容纳元素数量)
}
/* 初始化队列 */
func newArrayDeque(queCapacity int) *arrayDeque {
return &arrayDeque{
nums: make([]int, queCapacity),
queCapacity: queCapacity,
front: 0,
queSize: 0,
}
}
/* 获取双向队列的长度 */
func (q *arrayDeque) size() int {
return q.queSize
}
/* 判断双向队列是否为空 */
func (q *arrayDeque) isEmpty() bool {
return q.queSize == 0
}
/* 计算环形数组索引 */
func (q *arrayDeque) index(i int) int {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return (i + q.queCapacity) % q.queCapacity
}
/* 队首入队 */
func (q *arrayDeque) pushFirst(num int) {
if q.queSize == q.queCapacity {
fmt.Println("双向队列已满")
return
}
// 队首指针向左移动一位
// 通过取余操作,实现 front 越过数组头部后回到尾部
q.front = q.index(q.front - 1)
// 将 num 添加至队首
q.nums[q.front] = num
q.queSize++
}
/* 队尾入队 */
func (q *arrayDeque) pushLast(num int) {
if q.queSize == q.queCapacity {
fmt.Println("双向队列已满")
return
}
// 计算尾指针,指向队尾索引 + 1
rear := q.index(q.front + q.queSize)
// 将 num 添加至队首
q.nums[rear] = num
q.queSize++
}
/* 队首出队 */
func (q *arrayDeque) pollFirst() any {
num := q.peekFirst()
// 队首指针向后移动一位
q.front = q.index(q.front + 1)
q.queSize--
return num
}
/* 队尾出队 */
func (q *arrayDeque) pollLast() any {
num := q.peekLast()
q.queSize--
return num
}
/* 访问队首元素 */
func (q *arrayDeque) peekFirst() any {
if q.isEmpty() {
return nil
}
return q.nums[q.front]
}
/* 访问队尾元素 */
func (q *arrayDeque) peekLast() any {
if q.isEmpty() {
return nil
}
// 计算尾元素索引
last := q.index(q.front + q.queSize - 1)
return q.nums[last]
}
/* 获取 Slice 用于打印 */
func (q *arrayDeque) toSlice() []int {
// 仅转换有效长度范围内的列表元素
res := make([]int, q.queSize)
for i, j := 0, q.front; i < q.queSize; i++ {
res[i] = q.nums[q.index(j)]
j++
}
return res
}

View file

@ -49,6 +49,49 @@ func TestDeque(t *testing.T) {
fmt.Println("双向队列是否为空 =", isEmpty) fmt.Println("双向队列是否为空 =", isEmpty)
} }
func TestArrayDeque(t *testing.T) {
/* 初始化双向队列 */
// 在 Go 中,将 list 作为双向队列使用
deque := newArrayDeque(16)
/* 元素入队 */
deque.pushLast(3)
deque.pushLast(2)
deque.pushLast(5)
fmt.Print("双向队列 deque = ")
PrintSlice(deque.toSlice())
/* 访问元素 */
peekFirst := deque.peekFirst()
fmt.Println("队首元素 peekFirst =", peekFirst)
peekLast := deque.peekLast()
fmt.Println("队尾元素 peekLast =", peekLast)
/* 元素入队 */
deque.pushLast(4)
fmt.Print("元素 4 队尾入队后 deque = ")
PrintSlice(deque.toSlice())
deque.pushFirst(1)
fmt.Print("元素 1 队首入队后 deque = ")
PrintSlice(deque.toSlice())
/* 元素出队 */
pollFirst := deque.pollFirst()
fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ")
PrintSlice(deque.toSlice())
pollLast := deque.pollLast()
fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ")
PrintSlice(deque.toSlice())
/* 获取双向队列的长度 */
size := deque.size()
fmt.Println("双向队列长度 size =", size)
/* 判断双向队列是否为空 */
isEmpty := deque.isEmpty()
fmt.Println("双向队列是否为空 =", isEmpty)
}
func TestLinkedListDeque(t *testing.T) { func TestLinkedListDeque(t *testing.T) {
// 初始化队列 // 初始化队列
deque := newLinkedListDeque() deque := newLinkedListDeque()
@ -85,7 +128,7 @@ func TestLinkedListDeque(t *testing.T) {
fmt.Println("队是否为空 =", isEmpty) fmt.Println("队是否为空 =", isEmpty)
} }
// BenchmarkArrayQueue 67.92 ns/op in Mac M1 Pro // BenchmarkLinkedListDeque 67.92 ns/op in Mac M1 Pro
func BenchmarkLinkedListDeque(b *testing.B) { func BenchmarkLinkedListDeque(b *testing.B) {
deque := newLinkedListDeque() deque := newLinkedListDeque()
// use b.N for looping // use b.N for looping