2022-11-28 17:10:22 +00:00
|
|
|
// File: queue_test.go
|
|
|
|
// Created Time: 2022-11-28
|
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
package chapter_stack_and_queue
|
|
|
|
|
2022-12-03 12:25:24 +00:00
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueue(t *testing.T) {
|
|
|
|
/* 初始化队列 */
|
|
|
|
// 在 Go 中,将 list 作为队列来使用
|
|
|
|
queue := list.New()
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
queue.PushBack(1)
|
|
|
|
queue.PushBack(3)
|
|
|
|
queue.PushBack(2)
|
|
|
|
queue.PushBack(5)
|
|
|
|
queue.PushBack(4)
|
|
|
|
fmt.Print("队列 queue = ")
|
|
|
|
PrintList(queue)
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek := queue.Front()
|
|
|
|
fmt.Println("队首元素 peek =", peek.Value)
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
poll := queue.Front()
|
|
|
|
queue.Remove(poll)
|
|
|
|
fmt.Print("出队元素 poll = ", poll.Value, ",出队后 queue = ")
|
|
|
|
PrintList(queue)
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
size := queue.Len()
|
|
|
|
fmt.Println("队列长度 size =", size)
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
isEmpty := queue.Len() == 0
|
|
|
|
fmt.Println("队列是否为空 =", isEmpty)
|
|
|
|
}
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
func TestArrayQueue(t *testing.T) {
|
2022-11-29 02:42:09 +00:00
|
|
|
// 初始化队列,使用队列的通用接口
|
2022-11-28 17:10:22 +00:00
|
|
|
capacity := 10
|
2022-12-03 12:25:24 +00:00
|
|
|
queue := NewArrayQueue(capacity)
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 元素入队
|
|
|
|
queue.Offer(1)
|
|
|
|
queue.Offer(3)
|
2022-12-03 12:25:24 +00:00
|
|
|
queue.Offer(2)
|
2022-11-28 17:10:22 +00:00
|
|
|
queue.Offer(5)
|
2022-12-03 12:25:24 +00:00
|
|
|
queue.Offer(4)
|
|
|
|
fmt.Print("队列 queue = ")
|
|
|
|
PrintSlice(queue.toSlice())
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 访问队首元素
|
|
|
|
peek := queue.Peek()
|
2022-12-03 12:25:24 +00:00
|
|
|
fmt.Println("队首元素 peek =", peek)
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 元素出队
|
2022-12-03 12:25:24 +00:00
|
|
|
poll := queue.Poll()
|
|
|
|
fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ")
|
|
|
|
PrintSlice(queue.toSlice())
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 获取队的长度
|
|
|
|
size := queue.Size()
|
2022-12-03 12:25:24 +00:00
|
|
|
fmt.Println("队的长度 size =", size)
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 判断是否为空
|
|
|
|
isEmpty := queue.IsEmpty()
|
2022-12-03 12:25:24 +00:00
|
|
|
fmt.Println("队是否为空 =", isEmpty)
|
2022-11-28 17:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkedListQueue(t *testing.T) {
|
|
|
|
// 初始化队
|
2022-12-03 12:25:24 +00:00
|
|
|
queue := NewLinkedListQueue()
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 元素入队
|
|
|
|
queue.Offer(1)
|
|
|
|
queue.Offer(3)
|
2022-12-03 12:25:24 +00:00
|
|
|
queue.Offer(2)
|
2022-11-28 17:10:22 +00:00
|
|
|
queue.Offer(5)
|
2022-12-03 12:25:24 +00:00
|
|
|
queue.Offer(4)
|
|
|
|
fmt.Print("队列 queue = ")
|
|
|
|
PrintList(queue.toList())
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 访问队首元素
|
|
|
|
peek := queue.Peek()
|
2022-12-03 12:25:24 +00:00
|
|
|
fmt.Println("队首元素 peek =", peek)
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 元素出队
|
2022-12-03 12:25:24 +00:00
|
|
|
poll := queue.Poll()
|
|
|
|
fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ")
|
|
|
|
PrintList(queue.toList())
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 获取队的长度
|
|
|
|
size := queue.Size()
|
2022-12-03 12:25:24 +00:00
|
|
|
fmt.Println("队的长度 size =", size)
|
2022-11-28 17:10:22 +00:00
|
|
|
|
|
|
|
// 判断是否为空
|
|
|
|
isEmpty := queue.IsEmpty()
|
2022-12-03 12:25:24 +00:00
|
|
|
fmt.Println("队是否为空 =", isEmpty)
|
2022-11-28 17:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
|
|
|
|
func BenchmarkArrayQueue(b *testing.B) {
|
|
|
|
capacity := 1000
|
|
|
|
stack := NewArrayQueue(capacity)
|
|
|
|
// use b.N for looping
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stack.Offer(777)
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stack.Poll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
|
|
|
|
func BenchmarkLinkedQueue(b *testing.B) {
|
|
|
|
stack := NewLinkedListQueue()
|
|
|
|
// use b.N for looping
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stack.Offer(777)
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stack.Poll()
|
|
|
|
}
|
|
|
|
}
|