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:
Yudong Jin
2024-04-06 02:30:11 +08:00
committed by GitHub
parent 33d7f8a2e5
commit 5f7385c8a3
1875 changed files with 102923 additions and 18 deletions

View File

@@ -0,0 +1,140 @@
// File: array_queue.zig
// Created Time: 2023-01-15
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 基於環形陣列實現的佇列
pub fn ArrayQueue(comptime T: type) type {
return struct {
const Self = @This();
nums: []T = undefined, // 用於儲存佇列元素的陣列
cap: usize = 0, // 佇列容量
front: usize = 0, // 佇列首指標,指向佇列首元素
queSize: usize = 0, // 尾指標,指向佇列尾 + 1
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, // 記憶體分配器
// 建構子(分配記憶體+初始化陣列)
pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.cap = cap;
self.nums = try self.mem_allocator.alloc(T, self.cap);
@memset(self.nums, @as(T, 0));
}
// 析構函式(釋放記憶體)
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
// 獲取佇列的容量
pub fn capacity(self: *Self) usize {
return self.cap;
}
// 獲取佇列的長度
pub fn size(self: *Self) usize {
return self.queSize;
}
// 判斷佇列是否為空
pub fn isEmpty(self: *Self) bool {
return self.queSize == 0;
}
// 入列
pub fn push(self: *Self, num: T) !void {
if (self.size() == self.capacity()) {
std.debug.print("佇列已滿\n", .{});
return;
}
// 計算佇列尾指標,指向佇列尾索引 + 1
// 透過取餘操作實現 rear 越過陣列尾部後回到頭部
var rear = (self.front + self.queSize) % self.capacity();
// 在尾節點後新增 num
self.nums[rear] = num;
self.queSize += 1;
}
// 出列
pub fn pop(self: *Self) T {
var num = self.peek();
// 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部
self.front = (self.front + 1) % self.capacity();
self.queSize -= 1;
return num;
}
// 訪問佇列首元素
pub fn peek(self: *Self) T {
if (self.isEmpty()) @panic("佇列為空");
return self.nums[self.front];
}
// 返回陣列
pub fn toArray(self: *Self) ![]T {
// 僅轉換有效長度範圍內的串列元素
var res = try self.mem_allocator.alloc(T, self.size());
@memset(res, @as(T, 0));
var i: usize = 0;
var j: usize = self.front;
while (i < self.size()) : ({ i += 1; j += 1; }) {
res[i] = self.nums[j % self.capacity()];
}
return res;
}
};
}
// Driver Code
pub fn main() !void {
// 初始化佇列
var capacity: usize = 10;
var queue = ArrayQueue(i32){};
try queue.init(std.heap.page_allocator, capacity);
defer queue.deinit();
// 元素入列
try queue.push(1);
try queue.push(3);
try queue.push(2);
try queue.push(5);
try queue.push(4);
std.debug.print("佇列 queue = ", .{});
inc.PrintUtil.printArray(i32, try queue.toArray());
// 訪問佇列首元素
var peek = queue.peek();
std.debug.print("\n佇列首元素 peek = {}", .{peek});
// 元素出列
var pop = queue.pop();
std.debug.print("\n出列元素 pop = {},出列後 queue = ", .{pop});
inc.PrintUtil.printArray(i32, try queue.toArray());
// 獲取佇列的長度
var size = queue.size();
std.debug.print("\n佇列長度 size = {}", .{size});
// 判斷佇列是否為空
var is_empty = queue.isEmpty();
std.debug.print("\n佇列是否為空 = {}", .{is_empty});
// 測試環形陣列
var i: i32 = 0;
while (i < 10) : (i += 1) {
try queue.push(i);
_ = queue.pop();
std.debug.print("\n第 {} 輪入列 + 出列後 queue = ", .{i});
inc.PrintUtil.printArray(i32, try queue.toArray());
}
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,97 @@
// File: array_stack.zig
// Created Time: 2023-01-08
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 基於陣列實現的堆疊
pub fn ArrayStack(comptime T: type) type {
return struct {
const Self = @This();
stack: ?std.ArrayList(T) = null,
// 建構子(分配記憶體+初始化堆疊)
pub fn init(self: *Self, allocator: std.mem.Allocator) void {
if (self.stack == null) {
self.stack = std.ArrayList(T).init(allocator);
}
}
// 析構方法(釋放記憶體)
pub fn deinit(self: *Self) void {
if (self.stack == null) return;
self.stack.?.deinit();
}
// 獲取堆疊的長度
pub fn size(self: *Self) usize {
return self.stack.?.items.len;
}
// 判斷堆疊是否為空
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
// 訪問堆疊頂元素
pub fn peek(self: *Self) T {
if (self.isEmpty()) @panic("堆疊為空");
return self.stack.?.items[self.size() - 1];
}
// 入堆疊
pub fn push(self: *Self, num: T) !void {
try self.stack.?.append(num);
}
// 出堆疊
pub fn pop(self: *Self) T {
var num = self.stack.?.pop();
return num;
}
// 返回 ArrayList
pub fn toList(self: *Self) std.ArrayList(T) {
return self.stack.?;
}
};
}
// Driver Code
pub fn main() !void {
// 初始化堆疊
var stack = ArrayStack(i32){};
stack.init(std.heap.page_allocator);
// 延遲釋放記憶體
defer stack.deinit();
// 元素入堆疊
try stack.push(1);
try stack.push(3);
try stack.push(2);
try stack.push(5);
try stack.push(4);
std.debug.print("堆疊 stack = ", .{});
inc.PrintUtil.printList(i32, stack.toList());
// 訪問堆疊頂元素
var peek = stack.peek();
std.debug.print("\n堆疊頂元素 peek = {}", .{peek});
// 元素出堆疊
var top = stack.pop();
std.debug.print("\n出堆疊元素 pop = {},出堆疊後 stack = ", .{top});
inc.PrintUtil.printList(i32, stack.toList());
// 獲取堆疊的長度
var size = stack.size();
std.debug.print("\n堆疊的長度 size = {}", .{size});
// 判斷堆疊是否為空
var is_empty = stack.isEmpty();
std.debug.print("\n堆疊是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,51 @@
// File: deque.zig
// Created Time: 2023-01-15
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
// 初始化雙向佇列
const L = std.TailQueue(i32);
var deque = L{};
// 元素入列
var node1 = L.Node{ .data = 2 };
var node2 = L.Node{ .data = 5 };
var node3 = L.Node{ .data = 4 };
var node4 = L.Node{ .data = 3 };
var node5 = L.Node{ .data = 1 };
deque.append(&node1); // 新增至佇列尾
deque.append(&node2);
deque.append(&node3);
deque.prepend(&node4); // 新增至佇列首
deque.prepend(&node5);
std.debug.print("雙向佇列 deque = ", .{});
inc.PrintUtil.printQueue(i32, deque);
// 訪問元素
var peek_first = deque.first.?.data; // 佇列首元素
std.debug.print("\n佇列首元素 peek_first = {}", .{peek_first});
var peek_last = deque.last.?.data; // 佇列尾元素
std.debug.print("\n佇列尾元素 peek_last = {}", .{peek_last});
// 元素出列
var pop_first = deque.popFirst().?.data; // 佇列首元素出列
std.debug.print("\n佇列首出列元素 pop_first = {},佇列首出列後 deque = ", .{pop_first});
inc.PrintUtil.printQueue(i32, deque);
var pop_last = deque.pop().?.data; // 佇列尾元素出列
std.debug.print("\n佇列尾出列元素 pop_last = {},佇列尾出列後 deque = ", .{pop_last});
inc.PrintUtil.printQueue(i32, deque);
// 獲取雙向佇列的長度
var size = deque.len;
std.debug.print("\n雙向佇列長度 size = {}", .{size});
// 判斷雙向佇列是否為空
var is_empty = if (deque.len == 0) true else false;
std.debug.print("\n雙向佇列是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,207 @@
// File: linkedlist_deque.zig
// Created Time: 2023-01-15
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 雙向鏈結串列節點
pub fn ListNode(comptime T: type) type {
return struct {
const Self = @This();
val: T = undefined, // 節點值
next: ?*Self = null, // 後繼節點指標
prev: ?*Self = null, // 前驅節點指標
// Initialize a list node with specific value
pub fn init(self: *Self, x: i32) void {
self.val = x;
self.next = null;
self.prev = null;
}
};
}
// 基於雙向鏈結串列實現的雙向佇列
pub fn LinkedListDeque(comptime T: type) type {
return struct {
const Self = @This();
front: ?*ListNode(T) = null, // 頭節點 front
rear: ?*ListNode(T) = null, // 尾節點 rear
que_size: usize = 0, // 雙向佇列的長度
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, // 記憶體分配器
// 建構子(分配記憶體+初始化佇列)
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.front = null;
self.rear = null;
self.que_size = 0;
}
// 析構函式(釋放記憶體)
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
// 獲取雙向佇列的長度
pub fn size(self: *Self) usize {
return self.que_size;
}
// 判斷雙向佇列是否為空
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
// 入列操作
pub fn push(self: *Self, num: T, is_front: bool) !void {
var node = try self.mem_allocator.create(ListNode(T));
node.init(num);
// 若鏈結串列為空,則令 front 和 rear 都指向 node
if (self.isEmpty()) {
self.front = node;
self.rear = node;
// 佇列首入列操作
} else if (is_front) {
// 將 node 新增至鏈結串列頭部
self.front.?.prev = node;
node.next = self.front;
self.front = node; // 更新頭節點
// 佇列尾入列操作
} else {
// 將 node 新增至鏈結串列尾部
self.rear.?.next = node;
node.prev = self.rear;
self.rear = node; // 更新尾節點
}
self.que_size += 1; // 更新佇列長度
}
// 佇列首入列
pub fn pushFirst(self: *Self, num: T) !void {
try self.push(num, true);
}
// 佇列尾入列
pub fn pushLast(self: *Self, num: T) !void {
try self.push(num, false);
}
// 出列操作
pub fn pop(self: *Self, is_front: bool) T {
if (self.isEmpty()) @panic("雙向佇列為空");
var val: T = undefined;
// 佇列首出列操作
if (is_front) {
val = self.front.?.val; // 暫存頭節點值
// 刪除頭節點
var fNext = self.front.?.next;
if (fNext != null) {
fNext.?.prev = null;
self.front.?.next = null;
}
self.front = fNext; // 更新頭節點
// 佇列尾出列操作
} else {
val = self.rear.?.val; // 暫存尾節點值
// 刪除尾節點
var rPrev = self.rear.?.prev;
if (rPrev != null) {
rPrev.?.next = null;
self.rear.?.prev = null;
}
self.rear = rPrev; // 更新尾節點
}
self.que_size -= 1; // 更新佇列長度
return val;
}
// 佇列首出列
pub fn popFirst(self: *Self) T {
return self.pop(true);
}
// 佇列尾出列
pub fn popLast(self: *Self) T {
return self.pop(false);
}
// 訪問佇列首元素
pub fn peekFirst(self: *Self) T {
if (self.isEmpty()) @panic("雙向佇列為空");
return self.front.?.val;
}
// 訪問佇列尾元素
pub fn peekLast(self: *Self) T {
if (self.isEmpty()) @panic("雙向佇列為空");
return self.rear.?.val;
}
// 返回陣列用於列印
pub fn toArray(self: *Self) ![]T {
var node = self.front;
var res = try self.mem_allocator.alloc(T, self.size());
@memset(res, @as(T, 0));
var i: usize = 0;
while (i < res.len) : (i += 1) {
res[i] = node.?.val;
node = node.?.next;
}
return res;
}
};
}
// Driver Code
pub fn main() !void {
// 初始化雙向佇列
var deque = LinkedListDeque(i32){};
try deque.init(std.heap.page_allocator);
defer deque.deinit();
try deque.pushLast(3);
try deque.pushLast(2);
try deque.pushLast(5);
std.debug.print("雙向佇列 deque = ", .{});
inc.PrintUtil.printArray(i32, try deque.toArray());
// 訪問元素
var peek_first = deque.peekFirst();
std.debug.print("\n佇列首元素 peek_first = {}", .{peek_first});
var peek_last = deque.peekLast();
std.debug.print("\n佇列尾元素 peek_last = {}", .{peek_last});
// 元素入列
try deque.pushLast(4);
std.debug.print("\n元素 4 佇列尾入列後 deque = ", .{});
inc.PrintUtil.printArray(i32, try deque.toArray());
try deque.pushFirst(1);
std.debug.print("\n元素 1 佇列首入列後 deque = ", .{});
inc.PrintUtil.printArray(i32, try deque.toArray());
// 元素出列
var pop_last = deque.popLast();
std.debug.print("\n佇列尾出列元素 = {},佇列尾出列後 deque = ", .{pop_last});
inc.PrintUtil.printArray(i32, try deque.toArray());
var pop_first = deque.popFirst();
std.debug.print("\n佇列首出列元素 = {},佇列首出列後 deque = ", .{pop_first});
inc.PrintUtil.printArray(i32, try deque.toArray());
// 獲取雙向佇列的長度
var size = deque.size();
std.debug.print("\n雙向佇列長度 size = {}", .{size});
// 判斷雙向佇列是否為空
var is_empty = deque.isEmpty();
std.debug.print("\n雙向佇列是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,127 @@
// File: linkedlist_queue.zig
// Created Time: 2023-01-15
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 基於鏈結串列實現的佇列
pub fn LinkedListQueue(comptime T: type) type {
return struct {
const Self = @This();
front: ?*inc.ListNode(T) = null, // 頭節點 front
rear: ?*inc.ListNode(T) = null, // 尾節點 rear
que_size: usize = 0, // 佇列的長度
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, // 記憶體分配器
// 建構子(分配記憶體+初始化佇列)
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.front = null;
self.rear = null;
self.que_size = 0;
}
// 析構函式(釋放記憶體)
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
// 獲取佇列的長度
pub fn size(self: *Self) usize {
return self.que_size;
}
// 判斷佇列是否為空
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
// 訪問佇列首元素
pub fn peek(self: *Self) T {
if (self.size() == 0) @panic("佇列為空");
return self.front.?.val;
}
// 入列
pub fn push(self: *Self, num: T) !void {
// 在尾節點後新增 num
var node = try self.mem_allocator.create(inc.ListNode(T));
node.init(num);
// 如果佇列為空,則令頭、尾節點都指向該節點
if (self.front == null) {
self.front = node;
self.rear = node;
// 如果佇列不為空,則將該節點新增到尾節點後
} else {
self.rear.?.next = node;
self.rear = node;
}
self.que_size += 1;
}
// 出列
pub fn pop(self: *Self) T {
var num = self.peek();
// 刪除頭節點
self.front = self.front.?.next;
self.que_size -= 1;
return num;
}
// 將鏈結串列轉換為陣列
pub fn toArray(self: *Self) ![]T {
var node = self.front;
var res = try self.mem_allocator.alloc(T, self.size());
@memset(res, @as(T, 0));
var i: usize = 0;
while (i < res.len) : (i += 1) {
res[i] = node.?.val;
node = node.?.next;
}
return res;
}
};
}
// Driver Code
pub fn main() !void {
// 初始化佇列
var queue = LinkedListQueue(i32){};
try queue.init(std.heap.page_allocator);
defer queue.deinit();
// 元素入列
try queue.push(1);
try queue.push(3);
try queue.push(2);
try queue.push(5);
try queue.push(4);
std.debug.print("佇列 queue = ", .{});
inc.PrintUtil.printArray(i32, try queue.toArray());
// 訪問佇列首元素
var peek = queue.peek();
std.debug.print("\n佇列首元素 peek = {}", .{peek});
// 元素出列
var pop = queue.pop();
std.debug.print("\n出列元素 pop = {},出列後 queue = ", .{pop});
inc.PrintUtil.printArray(i32, try queue.toArray());
// 獲取佇列的長度
var size = queue.size();
std.debug.print("\n佇列長度 size = {}", .{size});
// 判斷佇列是否為空
var is_empty = queue.isEmpty();
std.debug.print("\n佇列是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,118 @@
// File: linkedlist_stack.zig
// Created Time: 2023-01-08
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 基於鏈結串列實現的堆疊
pub fn LinkedListStack(comptime T: type) type {
return struct {
const Self = @This();
stack_top: ?*inc.ListNode(T) = null, // 將頭節點作為堆疊頂
stk_size: usize = 0, // 堆疊的長度
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, // 記憶體分配器
// 建構子(分配記憶體+初始化堆疊)
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.stack_top = null;
self.stk_size = 0;
}
// 析構函式(釋放記憶體)
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
// 獲取堆疊的長度
pub fn size(self: *Self) usize {
return self.stk_size;
}
// 判斷堆疊是否為空
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
// 訪問堆疊頂元素
pub fn peek(self: *Self) T {
if (self.size() == 0) @panic("堆疊為空");
return self.stack_top.?.val;
}
// 入堆疊
pub fn push(self: *Self, num: T) !void {
var node = try self.mem_allocator.create(inc.ListNode(T));
node.init(num);
node.next = self.stack_top;
self.stack_top = node;
self.stk_size += 1;
}
// 出堆疊
pub fn pop(self: *Self) T {
var num = self.peek();
self.stack_top = self.stack_top.?.next;
self.stk_size -= 1;
return num;
}
// 將堆疊轉換為陣列
pub fn toArray(self: *Self) ![]T {
var node = self.stack_top;
var res = try self.mem_allocator.alloc(T, self.size());
@memset(res, @as(T, 0));
var i: usize = 0;
while (i < res.len) : (i += 1) {
res[res.len - i - 1] = node.?.val;
node = node.?.next;
}
return res;
}
};
}
// Driver Code
pub fn main() !void {
// 初始化堆疊
var stack = LinkedListStack(i32){};
try stack.init(std.heap.page_allocator);
// 延遲釋放記憶體
defer stack.deinit();
// 元素入堆疊
try stack.push(1);
try stack.push(3);
try stack.push(2);
try stack.push(5);
try stack.push(4);
std.debug.print("堆疊 stack = ", .{});
inc.PrintUtil.printArray(i32, try stack.toArray());
// 訪問堆疊頂元素
var peek = stack.peek();
std.debug.print("\n堆疊頂元素 top = {}", .{peek});
// 元素出堆疊
var pop = stack.pop();
std.debug.print("\n出堆疊元素 pop = {},出堆疊後 stack = ", .{pop});
inc.PrintUtil.printArray(i32, try stack.toArray());
// 獲取堆疊的長度
var size = stack.size();
std.debug.print("\n堆疊的長度 size = {}", .{size});
// 判斷堆疊是否為空
var is_empty = stack.isEmpty();
std.debug.print("\n堆疊是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,46 @@
// File: queue.zig
// Created Time: 2023-01-15
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
// 初始化佇列
const L = std.TailQueue(i32);
var queue = L{};
// 元素入列
var node1 = L.Node{ .data = 1 };
var node2 = L.Node{ .data = 3 };
var node3 = L.Node{ .data = 2 };
var node4 = L.Node{ .data = 5 };
var node5 = L.Node{ .data = 4 };
queue.append(&node1);
queue.append(&node2);
queue.append(&node3);
queue.append(&node4);
queue.append(&node5);
std.debug.print("佇列 queue = ", .{});
inc.PrintUtil.printQueue(i32, queue);
// 訪問佇列首元素
var peek = queue.first.?.data;
std.debug.print("\n佇列首元素 peek = {}", .{peek});
// 元素出列
var pop = queue.popFirst().?.data;
std.debug.print("\n出列元素 pop = {},出列後 queue = ", .{pop});
inc.PrintUtil.printQueue(i32, queue);
// 獲取佇列的長度
var size = queue.len;
std.debug.print("\n佇列長度 size = {}", .{size});
// 判斷佇列是否為空
var is_empty = if (queue.len == 0) true else false;
std.debug.print("\n佇列是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}

View File

@@ -0,0 +1,43 @@
// File: stack.zig
// Created Time: 2023-01-08
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
// 初始化堆疊
// 在 zig 中,推薦將 ArrayList 當作堆疊來使用
var stack = std.ArrayList(i32).init(std.heap.page_allocator);
// 延遲釋放記憶體
defer stack.deinit();
// 元素入堆疊
try stack.append(1);
try stack.append(3);
try stack.append(2);
try stack.append(5);
try stack.append(4);
std.debug.print("堆疊 stack = ", .{});
inc.PrintUtil.printList(i32, stack);
// 訪問堆疊頂元素
var peek = stack.items[stack.items.len - 1];
std.debug.print("\n堆疊頂元素 peek = {}", .{peek});
// 元素出堆疊
var pop = stack.pop();
std.debug.print("\n出堆疊元素 pop = {},出堆疊後 stack = ", .{pop});
inc.PrintUtil.printList(i32, stack);
// 獲取堆疊的長度
var size = stack.items.len;
std.debug.print("\n堆疊的長度 size = {}", .{size});
// 判斷堆疊是否為空
var is_empty = if (stack.items.len == 0) true else false;
std.debug.print("\n堆疊是否為空 = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}