code: update zig 0.14.1 for the chapter of array_and_linkedlist and computational_complexity (#1787)

* update zig array list chapter

* update not need change codes.

* fix some pr issues and update time space chapter
This commit is contained in:
MetaSky
2025-08-06 02:33:00 +08:00
committed by GitHub
parent 0918fd06f2
commit 803c0e09c7
22 changed files with 836 additions and 627 deletions

View File

@ -1,49 +0,0 @@
// File: ListNode.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
// 链表节点
pub fn ListNode(comptime T: type) type {
return struct {
const Self = @This();
val: T = 0,
next: ?*Self = null,
// Initialize a list node with specific value
pub fn init(self: *Self, x: i32) void {
self.val = x;
self.next = null;
}
};
}
// 将列表反序列化为链表
pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list: std.ArrayList(T)) !?*ListNode(T) {
var dum = try mem_allocator.create(ListNode(T));
dum.init(0);
var head = dum;
for (list.items) |val| {
var tmp = try mem_allocator.create(ListNode(T));
tmp.init(val);
head.next = tmp;
head = head.next.?;
}
return dum.next;
}
// 将数组反序列化为链表
pub fn arrToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*ListNode(T) {
var dum = try mem_allocator.create(ListNode(T));
dum.init(0);
var head = dum;
for (arr) |val| {
var tmp = try mem_allocator.create(ListNode(T));
tmp.init(val);
head.next = tmp;
head = head.next.?;
}
return dum.next;
}

View File

@ -8,45 +8,6 @@ pub const ListNode = ListUtil.ListNode;
pub const TreeUtil = @import("TreeNode.zig");
pub const TreeNode = TreeUtil.TreeNode;
// 打印数组
pub fn printArray(comptime T: type, nums: []T) void {
std.debug.print("[", .{});
if (nums.len > 0) {
for (nums, 0..) |num, j| {
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]" else ", " });
}
} else {
std.debug.print("]", .{});
}
}
// 打印列表
pub fn printList(comptime T: type, list: std.ArrayList(T)) void {
std.debug.print("[", .{});
if (list.items.len > 0) {
for (list.items, 0..) |value, i| {
std.debug.print("{}{s}", .{value, if (i == list.items.len-1) "]" else ", " });
}
} else {
std.debug.print("]", .{});
}
}
// 打印链表
pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void {
if (node == null) return;
var list = std.ArrayList(T).init(std.heap.page_allocator);
defer list.deinit();
var head = node;
while (head != null) {
try list.append(head.?.val);
head = head.?.next;
}
for (list.items, 0..) |value, i| {
std.debug.print("{}{s}", .{value, if (i == list.items.len-1) "\n" else "->" });
}
}
// 打印队列
pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void {
var node = queue.first;
@ -54,7 +15,7 @@ pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void {
var i: i32 = 0;
while (node != null) : (i += 1) {
var data = node.?.data;
std.debug.print("{}{s}", .{data, if (i == queue.len - 1) "]" else ", " });
std.debug.print("{}{s}", .{ data, if (i == queue.len - 1) "]" else ", " });
node = node.?.next;
}
}
@ -65,7 +26,7 @@ pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHas
while (it.next()) |kv| {
var key = kv.key_ptr.*;
var value = kv.value_ptr.*;
std.debug.print("{} -> {s}\n", .{key, value});
std.debug.print("{} -> {s}\n", .{ key, value });
}
}
@ -79,54 +40,3 @@ pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anyt
var root = try TreeUtil.arrToTree(T, mem_allocator, arr[0..len]);
try printTree(root, null, false);
}
// 打印二叉树
// This tree printer is borrowed from TECHIE DELIGHT
// https://www.techiedelight.com/c-program-print-binary-tree/
const Trunk = struct {
prev: ?*Trunk = null,
str: []const u8 = undefined,
pub fn init(self: *Trunk, prev: ?*Trunk, str: []const u8) void {
self.prev = prev;
self.str = str;
}
};
pub fn showTrunks(p: ?*Trunk) void {
if (p == null) return;
showTrunks(p.?.prev);
std.debug.print("{s}", .{p.?.str});
}
// 打印二叉树
pub fn printTree(root: ?*TreeNode(i32), prev: ?*Trunk, isRight: bool) !void {
if (root == null) {
return;
}
var prev_str = " ";
var trunk = Trunk{.prev = prev, .str = prev_str};
try printTree(root.?.right, &trunk, true);
if (prev == null) {
trunk.str = "———";
} else if (isRight) {
trunk.str = "/———";
prev_str = " |";
} else {
trunk.str = "\\———";
prev.?.str = prev_str;
}
showTrunks(&trunk);
std.debug.print(" {}\n", .{root.?.val});
if (prev) |_| {
prev.?.str = prev_str;
}
trunk.str = " |";
try printTree(root.?.left, &trunk, false);
}

View File

@ -1,63 +0,0 @@
// File: TreeNode.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
// 二叉树节点
pub fn TreeNode(comptime T: type) type {
return struct {
const Self = @This();
val: T = undefined, // 节点值
height: i32 = undefined, // 节点高度
left: ?*Self = null, // 左子节点指针
right: ?*Self = null, // 右子节点指针
// Initialize a tree node with specific value
pub fn init(self: *Self, x: i32) void {
self.val = x;
self.height = 0;
self.left = null;
self.right = null;
}
};
}
// 将数组反序列化为二叉树
pub fn arrToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
if (arr.len == 0) return null;
var root = try mem_allocator.create(TreeNode(T));
root.init(arr[0]);
const L = std.TailQueue(*TreeNode(T));
var que = L{};
var root_node = try mem_allocator.create(L.Node);
root_node.data = root;
que.append(root_node);
var index: usize = 0;
while (que.len > 0) {
var que_node = que.popFirst().?;
var node = que_node.data;
index += 1;
if (index >= arr.len) break;
if (index < arr.len) {
var tmp = try mem_allocator.create(TreeNode(T));
tmp.init(arr[index]);
node.left = tmp;
var tmp_node = try mem_allocator.create(L.Node);
tmp_node.data = node.left.?;
que.append(tmp_node);
}
index += 1;
if (index >= arr.len) break;
if (index < arr.len) {
var tmp = try mem_allocator.create(TreeNode(T));
tmp.init(arr[index]);
node.right = tmp;
var tmp_node = try mem_allocator.create(L.Node);
tmp_node.data = node.right.?;
que.append(tmp_node);
}
}
return root;
}

View File

@ -3,7 +3,5 @@
// Author: codingonion (coderonion@gmail.com)
pub const PrintUtil = @import("PrintUtil.zig");
pub const ListUtil = @import("ListNode.zig");
pub const ListNode = ListUtil.ListNode;
pub const TreeUtil = @import("TreeNode.zig");
pub const TreeNode = TreeUtil.TreeNode;
pub const TreeNode = TreeUtil.TreeNode;