mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
Add code source blocks to the chapter Graph.
Fix "函数" and "方法"
This commit is contained in:
@@ -17,7 +17,7 @@ pub fn MyList(comptime T: type) type {
|
||||
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);
|
||||
@@ -27,7 +27,7 @@ pub fn MyList(comptime T: type) type {
|
||||
std.mem.set(T, self.nums, @as(T, 0));
|
||||
}
|
||||
|
||||
// 析构函数(释放内存)
|
||||
// 析构方法(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
|
||||
@@ -26,7 +26,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
|
||||
const Self = @This();
|
||||
|
||||
// 构造函数
|
||||
// 构造方法
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
self.mem_allocator = allocator;
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
@@ -37,7 +37,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
}
|
||||
}
|
||||
|
||||
// 析构函数
|
||||
// 析构方法
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.bucket != null) self.bucket.?.deinit();
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn MaxHeap(comptime T: type) type {
|
||||
|
||||
maxHeap: ?std.ArrayList(T) = null, // 使用列表而非数组,这样无需考虑扩容问题
|
||||
|
||||
// 构造函数,根据输入列表建堆
|
||||
// 构造方法,根据输入列表建堆
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {
|
||||
if (self.maxHeap != null) return;
|
||||
self.maxHeap = std.ArrayList(T).init(allocator);
|
||||
@@ -25,7 +25,7 @@ pub fn MaxHeap(comptime T: type) type {
|
||||
}
|
||||
}
|
||||
|
||||
// 析构函数,释放内存
|
||||
// 析构方法,释放内存
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.maxHeap != null) self.maxHeap.?.deinit();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
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);
|
||||
@@ -28,7 +28,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
std.mem.set(T, self.nums, @as(T, 0));
|
||||
}
|
||||
|
||||
// 析构函数(释放内存)
|
||||
// 析构方法(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
|
||||
@@ -12,14 +12,14 @@ pub fn ArrayStack(comptime T: type) type {
|
||||
|
||||
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();
|
||||
|
||||
@@ -34,7 +34,7 @@ pub fn LinkedListDeque(comptime T: type) type {
|
||||
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);
|
||||
@@ -45,7 +45,7 @@ pub fn LinkedListDeque(comptime T: type) type {
|
||||
self.deqSize = 0;
|
||||
}
|
||||
|
||||
// 析构函数(释放内存)
|
||||
// 析构方法(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
|
||||
@@ -16,7 +16,7 @@ pub fn LinkedListQueue(comptime T: type) type {
|
||||
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);
|
||||
@@ -27,7 +27,7 @@ pub fn LinkedListQueue(comptime T: type) type {
|
||||
self.queSize = 0;
|
||||
}
|
||||
|
||||
// 析构函数(释放内存)
|
||||
// 析构方法(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
|
||||
@@ -15,7 +15,7 @@ pub fn LinkedListStack(comptime T: type) type {
|
||||
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);
|
||||
@@ -25,7 +25,7 @@ pub fn LinkedListStack(comptime T: type) type {
|
||||
self.stkSize = 0;
|
||||
}
|
||||
|
||||
// 析构函数(释放内存)
|
||||
// 析构方法(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn AVLTree(comptime T: type) type {
|
||||
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);
|
||||
@@ -22,7 +22,7 @@ pub fn AVLTree(comptime T: type) type {
|
||||
}
|
||||
}
|
||||
|
||||
// 析构函数
|
||||
// 析构方法
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
@@ -113,7 +113,7 @@ pub fn AVLTree(comptime T: type) type {
|
||||
return self.root;
|
||||
}
|
||||
|
||||
// 递归插入结点(辅助函数)
|
||||
// 递归插入结点(辅助方法)
|
||||
fn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {
|
||||
var node = node_;
|
||||
if (node == null) {
|
||||
@@ -142,7 +142,7 @@ pub fn AVLTree(comptime T: type) type {
|
||||
return self.root;
|
||||
}
|
||||
|
||||
// 递归删除结点(辅助函数)
|
||||
// 递归删除结点(辅助方法)
|
||||
fn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {
|
||||
var node = node_;
|
||||
if (node == null) return null;
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn BinarySearchTree(comptime T: type) type {
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
|
||||
// 构造函数
|
||||
// 构造方法
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator, nums: []T) !void {
|
||||
if (self.mem_arena == null) {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
@@ -24,7 +24,7 @@ pub fn BinarySearchTree(comptime T: type) type {
|
||||
self.root = try self.buildTree(nums, 0, nums.len - 1); // 构建二叉搜索树
|
||||
}
|
||||
|
||||
// 析构函数
|
||||
// 析构方法
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
|
||||
Reference in New Issue
Block a user