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

@ -0,0 +1,49 @@
// File: ListNode.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.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, allocator: std.mem.Allocator, list: std.ArrayList(T)) !?*ListNode(T) {
var dum = try allocator.create(ListNode(T));
dum.init(0);
var head = dum;
for (list.items) |val| {
var tmp = try 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

@ -0,0 +1,63 @@
// File: TreeNode.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.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, allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
if (arr.len == 0) return null;
var root = try allocator.create(TreeNode(T));
root.init(arr[0]);
const L = std.TailQueue(*TreeNode(T));
var que = L{};
var root_node = try allocator.create(L.Node);
root_node.data = root;
que.append(root_node);
var index: usize = 0;
while (que.len > 0) {
const que_node = que.popFirst().?;
var node = que_node.data;
index += 1;
if (index >= arr.len) break;
if (index < arr.len) {
var tmp = try allocator.create(TreeNode(T));
tmp.init(arr[index]);
node.left = tmp;
var tmp_node = try 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 allocator.create(TreeNode(T));
tmp.init(arr[index]);
node.right = tmp;
var tmp_node = try allocator.create(L.Node);
tmp_node.data = node.right.?;
que.append(tmp_node);
}
}
return root;
}

140
codes/zig/utils/format.zig Normal file
View File

@ -0,0 +1,140 @@
// File: format.zig
// Created Time: 2025-07-19
// Author: CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
const ListNode = @import("ListNode.zig").ListNode;
const TreeNode = @import("TreeNode.zig").TreeNode;
pub fn slice(items: anytype) SliceFormatter(@TypeOf(items)) {
return .{ .items = items };
}
pub fn SliceFormatter(comptime SliceType: type) type {
return struct {
const Self = @This();
items: SliceType,
pub fn format(
self: Self,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
try writer.writeAll("[");
if (self.items.len > 0) {
for (self.items, 0..) |item, i| {
try std.fmt.format(writer, "{}", .{item});
if (i != self.items.len - 1) {
try writer.writeAll(", ");
}
}
}
try writer.writeAll("]");
}
};
}
pub fn linkedList(comptime T: type, head: *const ListNode(T)) LinkedListFormatter(T) {
return .{ .head = head };
}
pub fn LinkedListFormatter(comptime T: type) type {
return struct {
const Self = @This();
head: *const ListNode(T),
pub fn format(
self: Self,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
try printLinkedList(self.head, writer);
}
pub fn printLinkedList(head: *const ListNode(T), writer: anytype) !void {
try std.fmt.format(writer, "{}", .{head.val});
if (head.next) |next_node| {
try writer.writeAll("->");
try printLinkedList(next_node, writer);
}
}
};
}
pub fn tree(comptime T: type, root: ?*const TreeNode(T)) TreeFormatter(T) {
return .{ .root = root };
}
pub fn TreeFormatter(comptime T: type) type {
return struct {
const Self = @This();
root: ?*const TreeNode(T),
pub fn format(
self: Self,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
try printTree(self.root, null, false, writer);
}
// 打印二叉树
fn printTree(root: ?*const TreeNode(T), prev: ?*Trunk, isRight: bool, writer: anytype) !void {
if (root == null) {
return;
}
var prev_str = " ";
var trunk = Trunk{ .prev = prev, .str = prev_str };
try printTree(root.?.right, &trunk, true, writer);
if (prev == null) {
trunk.str = "———";
} else if (isRight) {
trunk.str = "/———";
prev_str = " |";
} else {
trunk.str = "\\———";
prev.?.str = prev_str;
}
try showTrunks(&trunk, writer);
try std.fmt.format(writer, "{d}\n", .{root.?.val});
if (prev) |_| {
prev.?.str = prev_str;
}
trunk.str = " |";
try printTree(root.?.left, &trunk, false, writer);
}
// 打印二叉树
// 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, writer: anytype) !void {
if (p == null) return;
try showTrunks(p.?.prev, writer);
try std.fmt.format(writer, "{s}", .{p.?.str});
}
};
}

View File

@ -0,0 +1,8 @@
// File: format.zig
// Created Time: 2025-07-15
// Author: CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
pub const fmt = @import("format.zig");
pub const ListNode = @import("ListNode.zig").ListNode;
pub const TreeNode = @import("TreeNode.zig").TreeNode;