mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-10 19:10:57 +00:00
feat: Traditional Chinese version (#1163)
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
This commit is contained in:
@@ -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) popFirst() any {
|
||||
num := q.peekFirst()
|
||||
// 佇列首指標向後移動一位
|
||||
q.front = q.index(q.front + 1)
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* 佇列尾出列 */
|
||||
func (q *arrayDeque) popLast() 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
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// File: array_queue.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* 基於環形陣列實現的佇列 */
|
||||
type arrayQueue struct {
|
||||
nums []int // 用於儲存佇列元素的陣列
|
||||
front int // 佇列首指標,指向佇列首元素
|
||||
queSize int // 佇列長度
|
||||
queCapacity int // 佇列容量(即最大容納元素數量)
|
||||
}
|
||||
|
||||
/* 初始化佇列 */
|
||||
func newArrayQueue(queCapacity int) *arrayQueue {
|
||||
return &arrayQueue{
|
||||
nums: make([]int, queCapacity),
|
||||
queCapacity: queCapacity,
|
||||
front: 0,
|
||||
queSize: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
func (q *arrayQueue) size() int {
|
||||
return q.queSize
|
||||
}
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
func (q *arrayQueue) isEmpty() bool {
|
||||
return q.queSize == 0
|
||||
}
|
||||
|
||||
/* 入列 */
|
||||
func (q *arrayQueue) push(num int) {
|
||||
// 當 rear == queCapacity 表示佇列已滿
|
||||
if q.queSize == q.queCapacity {
|
||||
return
|
||||
}
|
||||
// 計算佇列尾指標,指向佇列尾索引 + 1
|
||||
// 透過取餘操作實現 rear 越過陣列尾部後回到頭部
|
||||
rear := (q.front + q.queSize) % q.queCapacity
|
||||
// 將 num 新增至佇列尾
|
||||
q.nums[rear] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
/* 出列 */
|
||||
func (q *arrayQueue) pop() any {
|
||||
num := q.peek()
|
||||
// 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部
|
||||
q.front = (q.front + 1) % q.queCapacity
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
func (q *arrayQueue) peek() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
return q.nums[q.front]
|
||||
}
|
||||
|
||||
/* 獲取 Slice 用於列印 */
|
||||
func (q *arrayQueue) toSlice() []int {
|
||||
rear := (q.front + q.queSize)
|
||||
if rear >= q.queCapacity {
|
||||
rear %= q.queCapacity
|
||||
return append(q.nums[q.front:], q.nums[:rear]...)
|
||||
}
|
||||
return q.nums[q.front:rear]
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// File: array_stack.go
|
||||
// Created Time: 2022-11-26
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* 基於陣列實現的堆疊 */
|
||||
type arrayStack struct {
|
||||
data []int // 資料
|
||||
}
|
||||
|
||||
/* 初始化堆疊 */
|
||||
func newArrayStack() *arrayStack {
|
||||
return &arrayStack{
|
||||
// 設定堆疊的長度為 0,容量為 16
|
||||
data: make([]int, 0, 16),
|
||||
}
|
||||
}
|
||||
|
||||
/* 堆疊的長度 */
|
||||
func (s *arrayStack) size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
|
||||
/* 堆疊是否為空 */
|
||||
func (s *arrayStack) isEmpty() bool {
|
||||
return s.size() == 0
|
||||
}
|
||||
|
||||
/* 入堆疊 */
|
||||
func (s *arrayStack) push(v int) {
|
||||
// 切片會自動擴容
|
||||
s.data = append(s.data, v)
|
||||
}
|
||||
|
||||
/* 出堆疊 */
|
||||
func (s *arrayStack) pop() any {
|
||||
val := s.peek()
|
||||
s.data = s.data[:len(s.data)-1]
|
||||
return val
|
||||
}
|
||||
|
||||
/* 獲取堆疊頂元素 */
|
||||
func (s *arrayStack) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
val := s.data[len(s.data)-1]
|
||||
return val
|
||||
}
|
||||
|
||||
/* 獲取 Slice 用於列印 */
|
||||
func (s *arrayStack) toSlice() []int {
|
||||
return s.data
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// File: deque_test.go
|
||||
// Created Time: 2022-11-29
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestDeque(t *testing.T) {
|
||||
/* 初始化雙向佇列 */
|
||||
// 在 Go 中,將 list 作為雙向佇列使用
|
||||
deque := list.New()
|
||||
|
||||
/* 元素入列 */
|
||||
deque.PushBack(2)
|
||||
deque.PushBack(5)
|
||||
deque.PushBack(4)
|
||||
deque.PushFront(3)
|
||||
deque.PushFront(1)
|
||||
fmt.Print("雙向佇列 deque = ")
|
||||
PrintList(deque)
|
||||
|
||||
/* 訪問元素 */
|
||||
front := deque.Front()
|
||||
fmt.Println("佇列首元素 front =", front.Value)
|
||||
rear := deque.Back()
|
||||
fmt.Println("佇列尾元素 rear =", rear.Value)
|
||||
|
||||
/* 元素出列 */
|
||||
deque.Remove(front)
|
||||
fmt.Print("佇列首出列元素 front = ", front.Value, ",佇列首出列後 deque = ")
|
||||
PrintList(deque)
|
||||
deque.Remove(rear)
|
||||
fmt.Print("佇列尾出列元素 rear = ", rear.Value, ",佇列尾出列後 deque = ")
|
||||
PrintList(deque)
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
size := deque.Len()
|
||||
fmt.Println("雙向佇列長度 size =", size)
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
isEmpty := deque.Len() == 0
|
||||
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())
|
||||
|
||||
/* 元素出列 */
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("佇列首出列元素 popFirst = ", popFirst, ",佇列首出列後 deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("佇列尾出列元素 popLast = ", popLast, ",佇列尾出列後 deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
size := deque.size()
|
||||
fmt.Println("雙向佇列長度 size =", size)
|
||||
|
||||
/* 判斷雙向佇列是否為空 */
|
||||
isEmpty := deque.isEmpty()
|
||||
fmt.Println("雙向佇列是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
func TestLinkedListDeque(t *testing.T) {
|
||||
// 初始化佇列
|
||||
deque := newLinkedListDeque()
|
||||
|
||||
// 元素入列
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
deque.pushLast(4)
|
||||
deque.pushFirst(3)
|
||||
deque.pushFirst(1)
|
||||
fmt.Print("佇列 deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// 訪問佇列首元素
|
||||
front := deque.peekFirst()
|
||||
fmt.Println("佇列首元素 front =", front)
|
||||
rear := deque.peekLast()
|
||||
fmt.Println("佇列尾元素 rear =", rear)
|
||||
|
||||
// 元素出列
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("佇列首出列元素 popFirst = ", popFirst, ",佇列首出列後 deque = ")
|
||||
PrintList(deque.toList())
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("佇列尾出列元素 popLast = ", popLast, ",佇列尾出列後 deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// 獲取隊的長度
|
||||
size := deque.size()
|
||||
fmt.Println("隊的長度 size =", size)
|
||||
|
||||
// 判斷是否為空
|
||||
isEmpty := deque.isEmpty()
|
||||
fmt.Println("隊是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkLinkedListDeque 67.92 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedListDeque(b *testing.B) {
|
||||
deque := newLinkedListDeque()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
deque.pushLast(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
deque.popFirst()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// File: linkedlist_deque.go
|
||||
// Created Time: 2022-11-29
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* 基於雙向鏈結串列實現的雙向佇列 */
|
||||
type linkedListDeque struct {
|
||||
// 使用內建包 list
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* 初始化雙端佇列 */
|
||||
func newLinkedListDeque() *linkedListDeque {
|
||||
return &linkedListDeque{
|
||||
data: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
/* 佇列首元素入列 */
|
||||
func (s *linkedListDeque) pushFirst(value any) {
|
||||
s.data.PushFront(value)
|
||||
}
|
||||
|
||||
/* 佇列尾元素入列 */
|
||||
func (s *linkedListDeque) pushLast(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
/* 佇列首元素出列 */
|
||||
func (s *linkedListDeque) popFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 佇列尾元素出列 */
|
||||
func (s *linkedListDeque) popLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
func (s *linkedListDeque) peekFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 訪問佇列尾元素 */
|
||||
func (s *linkedListDeque) peekLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
func (s *linkedListDeque) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
func (s *linkedListDeque) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* 獲取 List 用於列印 */
|
||||
func (s *linkedListDeque) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// File: linkedlist_queue.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* 基於鏈結串列實現的佇列 */
|
||||
type linkedListQueue struct {
|
||||
// 使用內建包 list 來實現佇列
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* 初始化佇列 */
|
||||
func newLinkedListQueue() *linkedListQueue {
|
||||
return &linkedListQueue{
|
||||
data: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
/* 入列 */
|
||||
func (s *linkedListQueue) push(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
/* 出列 */
|
||||
func (s *linkedListQueue) pop() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
func (s *linkedListQueue) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
func (s *linkedListQueue) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
func (s *linkedListQueue) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* 獲取 List 用於列印 */
|
||||
func (s *linkedListQueue) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// File: linkedlist_stack.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* 基於鏈結串列實現的堆疊 */
|
||||
type linkedListStack struct {
|
||||
// 使用內建包 list 來實現堆疊
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* 初始化堆疊 */
|
||||
func newLinkedListStack() *linkedListStack {
|
||||
return &linkedListStack{
|
||||
data: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
/* 入堆疊 */
|
||||
func (s *linkedListStack) push(value int) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
/* 出堆疊 */
|
||||
func (s *linkedListStack) pop() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 訪問堆疊頂元素 */
|
||||
func (s *linkedListStack) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
func (s *linkedListStack) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* 判斷堆疊是否為空 */
|
||||
func (s *linkedListStack) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* 獲取 List 用於列印 */
|
||||
func (s *linkedListStack) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
// File: queue_test.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
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)
|
||||
|
||||
/* 元素出列 */
|
||||
pop := queue.Front()
|
||||
queue.Remove(pop)
|
||||
fmt.Print("出列元素 pop = ", pop.Value, ",出列後 queue = ")
|
||||
PrintList(queue)
|
||||
|
||||
/* 獲取佇列的長度 */
|
||||
size := queue.Len()
|
||||
fmt.Println("佇列長度 size =", size)
|
||||
|
||||
/* 判斷佇列是否為空 */
|
||||
isEmpty := queue.Len() == 0
|
||||
fmt.Println("佇列是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayQueue(t *testing.T) {
|
||||
// 初始化佇列,使用佇列的通用介面
|
||||
capacity := 10
|
||||
queue := newArrayQueue(capacity)
|
||||
|
||||
// 元素入列
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
fmt.Print("佇列 queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// 訪問佇列首元素
|
||||
peek := queue.peek()
|
||||
fmt.Println("佇列首元素 peek =", peek)
|
||||
|
||||
// 元素出列
|
||||
pop := queue.pop()
|
||||
fmt.Print("出列元素 pop = ", pop, ", 出列後 queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// 獲取隊的長度
|
||||
size := queue.size()
|
||||
fmt.Println("隊的長度 size =", size)
|
||||
|
||||
// 判斷是否為空
|
||||
isEmpty := queue.isEmpty()
|
||||
fmt.Println("隊是否為空 =", isEmpty)
|
||||
|
||||
/* 測試環形陣列 */
|
||||
for i := 0; i < 10; i++ {
|
||||
queue.push(i)
|
||||
queue.pop()
|
||||
fmt.Print("第", i, "輪入列 + 出列後 queue =")
|
||||
PrintSlice(queue.toSlice())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkedListQueue(t *testing.T) {
|
||||
// 初始化隊
|
||||
queue := newLinkedListQueue()
|
||||
|
||||
// 元素入列
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
fmt.Print("佇列 queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// 訪問佇列首元素
|
||||
peek := queue.peek()
|
||||
fmt.Println("佇列首元素 peek =", peek)
|
||||
|
||||
// 元素出列
|
||||
pop := queue.pop()
|
||||
fmt.Print("出列元素 pop = ", pop, ", 出列後 queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// 獲取隊的長度
|
||||
size := queue.size()
|
||||
fmt.Println("隊的長度 size =", size)
|
||||
|
||||
// 判斷是否為空
|
||||
isEmpty := queue.isEmpty()
|
||||
fmt.Println("隊是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
|
||||
func BenchmarkArrayQueue(b *testing.B) {
|
||||
capacity := 1000
|
||||
queue := newArrayQueue(capacity)
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.pop()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedQueue(b *testing.B) {
|
||||
queue := newLinkedListQueue()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.pop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// File: stack_test.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
/* 初始化堆疊 */
|
||||
// 在 Go 中,推薦將 Slice 當作堆疊來使用
|
||||
var stack []int
|
||||
|
||||
/* 元素入堆疊 */
|
||||
stack = append(stack, 1)
|
||||
stack = append(stack, 3)
|
||||
stack = append(stack, 2)
|
||||
stack = append(stack, 5)
|
||||
stack = append(stack, 4)
|
||||
fmt.Print("堆疊 stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* 訪問堆疊頂元素 */
|
||||
peek := stack[len(stack)-1]
|
||||
fmt.Println("堆疊頂元素 peek =", peek)
|
||||
|
||||
/* 元素出堆疊 */
|
||||
pop := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
fmt.Print("出堆疊元素 pop = ", pop, ",出堆疊後 stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
size := len(stack)
|
||||
fmt.Println("堆疊的長度 size =", size)
|
||||
|
||||
/* 判斷是否為空 */
|
||||
isEmpty := len(stack) == 0
|
||||
fmt.Println("堆疊是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayStack(t *testing.T) {
|
||||
// 初始化堆疊, 使用介面承接
|
||||
stack := newArrayStack()
|
||||
|
||||
// 元素入堆疊
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
fmt.Print("堆疊 stack = ")
|
||||
PrintSlice(stack.toSlice())
|
||||
|
||||
// 訪問堆疊頂元素
|
||||
peek := stack.peek()
|
||||
fmt.Println("堆疊頂元素 peek =", peek)
|
||||
|
||||
// 元素出堆疊
|
||||
pop := stack.pop()
|
||||
fmt.Print("出堆疊元素 pop = ", pop, ", 出堆疊後 stack = ")
|
||||
PrintSlice(stack.toSlice())
|
||||
|
||||
// 獲取堆疊的長度
|
||||
size := stack.size()
|
||||
fmt.Println("堆疊的長度 size =", size)
|
||||
|
||||
// 判斷是否為空
|
||||
isEmpty := stack.isEmpty()
|
||||
fmt.Println("堆疊是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
func TestLinkedListStack(t *testing.T) {
|
||||
// 初始化堆疊
|
||||
stack := newLinkedListStack()
|
||||
// 元素入堆疊
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
fmt.Print("堆疊 stack = ")
|
||||
PrintList(stack.toList())
|
||||
|
||||
// 訪問堆疊頂元素
|
||||
peek := stack.peek()
|
||||
fmt.Println("堆疊頂元素 peek =", peek)
|
||||
|
||||
// 元素出堆疊
|
||||
pop := stack.pop()
|
||||
fmt.Print("出堆疊元素 pop = ", pop, ", 出堆疊後 stack = ")
|
||||
PrintList(stack.toList())
|
||||
|
||||
// 獲取堆疊的長度
|
||||
size := stack.size()
|
||||
fmt.Println("堆疊的長度 size =", size)
|
||||
|
||||
// 判斷是否為空
|
||||
isEmpty := stack.isEmpty()
|
||||
fmt.Println("堆疊是否為空 =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkArrayStack 8 ns/op in Mac M1 Pro
|
||||
func BenchmarkArrayStack(b *testing.B) {
|
||||
stack := newArrayStack()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.pop()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedListStack(b *testing.B) {
|
||||
stack := newLinkedListStack()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.pop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user