Skip to content

Commit 803c0e0

Browse files
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
1 parent 0918fd0 commit 803c0e0

File tree

22 files changed

+822
-613
lines changed

22 files changed

+822
-613
lines changed

codes/zig/.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
zig-out/
2-
zig-cache/
1+
zig-out
2+
zig-cache
3+
.zig-cache
4+
!/.vscode/

codes/zig/.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug",
6+
"type": "lldb",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/zig-out/bin/${fileBasenameNoExtension}",
9+
"args": [],
10+
"cwd": "${workspaceFolder}",
11+
"preLaunchTask": "build"
12+
}
13+
]
14+
}

codes/zig/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"zig.testArgs": ["build", "test", "-Dtest-filter=${filter}"]
3+
}

codes/zig/.vscode/tasks.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"type": "shell",
7+
"command": "zig build",
8+
}
9+
]
10+
}

codes/zig/build.zig

Lines changed: 142 additions & 194 deletions
Large diffs are not rendered by default.
Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
// File: array.zig
22
// Created Time: 2023-01-07
3-
// Author: codingonion (coderonion@gmail.com)
3+
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
44

55
const std = @import("std");
6-
const inc = @import("include");
6+
const utils = @import("utils");
77

88
// 随机访问元素
9-
pub fn randomAccess(nums: []i32) i32 {
9+
pub fn randomAccess(nums: []const i32) i32 {
1010
// 在区间 [0, nums.len) 中随机抽取一个整数
11-
var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);
11+
const random_index = std.crypto.random.intRangeLessThan(usize, 0, nums.len);
1212
// 获取并返回随机元素
13-
var randomNum = nums[randomIndex];
13+
const randomNum = nums[random_index];
1414
return randomNum;
1515
}
1616

1717
// 扩展数组长度
18-
pub fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {
18+
pub fn extend(allocator: std.mem.Allocator, nums: []const i32, enlarge: usize) ![]i32 {
1919
// 初始化一个扩展长度后的数组
20-
var res = try mem_allocator.alloc(i32, nums.len + enlarge);
20+
const res = try allocator.alloc(i32, nums.len + enlarge);
2121
@memset(res, 0);
22+
2223
// 将原数组中的所有元素复制到新数组
23-
std.mem.copy(i32, res, nums);
24+
std.mem.copyForwards(i32, res, nums);
25+
2426
// 返回扩展后的新数组
2527
return res;
2628
}
@@ -46,18 +48,26 @@ pub fn remove(nums: []i32, index: usize) void {
4648
}
4749

4850
// 遍历数组
49-
pub fn traverse(nums: []i32) void {
51+
pub fn traverse(nums: []const i32) void {
5052
var count: i32 = 0;
53+
5154
// 通过索引遍历数组
52-
var i: i32 = 0;
55+
var i: usize = 0;
5356
while (i < nums.len) : (i += 1) {
5457
count += nums[i];
5558
}
56-
count = 0;
59+
5760
// 直接遍历数组元素
61+
count = 0;
5862
for (nums) |num| {
5963
count += num;
6064
}
65+
66+
// 同时遍历数据索引和元素
67+
for (nums, 0..) |num, index| {
68+
count += nums[index];
69+
count += num;
70+
}
6171
}
6272

6373
// 在数组中查找指定元素
@@ -69,49 +79,53 @@ pub fn find(nums: []i32, target: i32) i32 {
6979
}
7080

7181
// Driver Code
72-
pub fn main() !void {
73-
// 初始化内存分配器
74-
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
75-
defer mem_arena.deinit();
76-
const mem_allocator = mem_arena.allocator();
77-
82+
pub fn run() !void {
7883
// 初始化数组
79-
var arr = [_]i32{0} ** 5;
80-
std.debug.print("数组 arr = ", .{});
81-
inc.PrintUtil.printArray(i32, &arr);
84+
const arr = [_]i32{0} ** 5;
85+
std.debug.print("数组 arr = {}\n", .{utils.fmt.slice(&arr)});
8286

87+
// 数组切片
8388
var array = [_]i32{ 1, 3, 2, 5, 4 };
8489
var known_at_runtime_zero: usize = 0;
85-
var nums = array[known_at_runtime_zero..];
86-
std.debug.print("\n数组 nums = ", .{});
87-
inc.PrintUtil.printArray(i32, nums);
90+
_ = &known_at_runtime_zero;
91+
var nums = array[known_at_runtime_zero..array.len]; // 通过 known_at_runtime_zero 运行时变量将指针变切片
92+
std.debug.print("数组 nums = {}\n", .{utils.fmt.slice(nums)});
8893

8994
// 随机访问
90-
var randomNum = randomAccess(nums);
91-
std.debug.print("\n在 nums 中获取随机元素 {}", .{randomNum});
95+
const randomNum = randomAccess(nums);
96+
std.debug.print("在 nums 中获取随机元素 {}\n", .{randomNum});
97+
98+
// 初始化内存分配器
99+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
100+
defer arena.deinit();
101+
const allocator = arena.allocator();
92102

93103
// 长度扩展
94-
nums = try extend(mem_allocator, nums, 3);
95-
std.debug.print("\n将数组长度扩展至 8 ,得到 nums = ", .{});
96-
inc.PrintUtil.printArray(i32, nums);
104+
nums = try extend(allocator, nums, 3);
105+
std.debug.print("将数组长度扩展至 8 ,得到 nums = {}\n", .{utils.fmt.slice(nums)});
97106

98107
// 插入元素
99108
insert(nums, 6, 3);
100-
std.debug.print("\n在索引 3 处插入数字 6 ,得到 nums = ", .{});
101-
inc.PrintUtil.printArray(i32, nums);
109+
std.debug.print("在索引 3 处插入数字 6 ,得到 nums = {}\n", .{utils.fmt.slice(nums)});
102110

103111
// 删除元素
104112
remove(nums, 2);
105-
std.debug.print("\n删除索引 2 处的元素,得到 nums = ", .{});
106-
inc.PrintUtil.printArray(i32, nums);
113+
std.debug.print("删除索引 2 处的元素,得到 nums = {}\n", .{utils.fmt.slice(nums)});
107114

108115
// 遍历数组
109116
traverse(nums);
110117

111118
// 查找元素
112-
var index = find(nums, 3);
113-
std.debug.print("\n nums 中查找元素 3 ,得到索引 = {}\n", .{index});
119+
const index = find(nums, 3);
120+
std.debug.print(" nums 中查找元素 3 ,得到索引 = {}\n", .{index});
114121

115-
_ = try std.io.getStdIn().reader().readByte();
122+
std.debug.print("\n", .{});
116123
}
117124

125+
pub fn main() !void {
126+
try run();
127+
}
128+
129+
test "array" {
130+
try run();
131+
}
Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,106 @@
11
// File: linked_list.zig
22
// Created Time: 2023-01-07
3-
// Author: codingonion (coderonion@gmail.com)
3+
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
44

55
const std = @import("std");
6-
const inc = @import("include");
6+
const utils = @import("utils");
7+
const ListNode = utils.ListNode;
78

89
// 在链表的节点 n0 之后插入节点 P
9-
pub fn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {
10-
var n1 = n0.?.next;
11-
P.?.next = n1;
12-
n0.?.next = P;
10+
pub fn insert(comptime T: type, n0: *ListNode(T), P: *ListNode(T)) void {
11+
const n1 = n0.next;
12+
P.next = n1;
13+
n0.next = P;
1314
}
1415

1516
// 删除链表的节点 n0 之后的首个节点
16-
pub fn remove(n0: ?*inc.ListNode(i32)) void {
17-
if (n0.?.next == null) return;
18-
// n0 -> P -> n1
19-
var P = n0.?.next;
20-
var n1 = P.?.next;
21-
n0.?.next = n1;
17+
pub fn remove(comptime T: type, n0: *ListNode(T)) void {
18+
// n0 -> P -> n1 => n0 -> n1
19+
const P = n0.next;
20+
const n1 = P.?.next;
21+
n0.next = n1;
2222
}
2323

2424
// 访问链表中索引为 index 的节点
25-
pub fn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {
26-
var head = node;
25+
pub fn access(comptime T: type, node: *ListNode(T), index: i32) ?*ListNode(T) {
26+
var head: ?*ListNode(T) = node;
2727
var i: i32 = 0;
2828
while (i < index) : (i += 1) {
29-
head = head.?.next;
30-
if (head == null) return null;
29+
if (head) |cur| {
30+
head = cur.next;
31+
} else {
32+
return null;
33+
}
3134
}
3235
return head;
3336
}
3437

3538
// 在链表中查找值为 target 的首个节点
36-
pub fn find(node: ?*inc.ListNode(i32), target: i32) i32 {
37-
var head = node;
39+
pub fn find(comptime T: type, node: *ListNode(T), target: T) i32 {
40+
var head: ?*ListNode(T) = node;
3841
var index: i32 = 0;
39-
while (head != null) {
40-
if (head.?.val == target) return index;
41-
head = head.?.next;
42+
while (head) |cur| {
43+
if (cur.val == target) return index;
44+
head = cur.next;
4245
index += 1;
4346
}
4447
return -1;
4548
}
4649

4750
// Driver Code
48-
pub fn main() !void {
49-
// 初始化链表
50-
// 初始化各个节点
51-
var n0 = inc.ListNode(i32){.val = 1};
52-
var n1 = inc.ListNode(i32){.val = 3};
53-
var n2 = inc.ListNode(i32){.val = 2};
54-
var n3 = inc.ListNode(i32){.val = 5};
55-
var n4 = inc.ListNode(i32){.val = 4};
51+
pub fn run() void {
52+
// 初始化各个节点
53+
var n0 = ListNode(i32){ .val = 1 };
54+
var n1 = ListNode(i32){ .val = 3 };
55+
var n2 = ListNode(i32){ .val = 2 };
56+
var n3 = ListNode(i32){ .val = 5 };
57+
var n4 = ListNode(i32){ .val = 4 };
5658
// 构建节点之间的引用
5759
n0.next = &n1;
5860
n1.next = &n2;
5961
n2.next = &n3;
6062
n3.next = &n4;
61-
std.debug.print("初始化的链表为", .{});
62-
try inc.PrintUtil.printLinkedList(i32, &n0);
63+
std.debug.print(
64+
"初始化的链表为 {}\n",
65+
.{utils.fmt.linkedList(i32, &n0)},
66+
);
6367

6468
// 插入节点
65-
var tmp = inc.ListNode(i32){.val = 0};
66-
insert(&n0, &tmp);
67-
std.debug.print("插入节点后的链表为", .{});
68-
try inc.PrintUtil.printLinkedList(i32, &n0);
69+
var tmp = ListNode(i32){ .val = 0 };
70+
insert(i32, &n0, &tmp);
71+
std.debug.print(
72+
"插入节点后的链表为 {}\n",
73+
.{utils.fmt.linkedList(i32, &n0)},
74+
);
6975

7076
// 删除节点
71-
remove(&n0);
72-
std.debug.print("删除节点后的链表为", .{});
73-
try inc.PrintUtil.printLinkedList(i32, &n0);
77+
remove(i32, &n0);
78+
std.debug.print(
79+
"删除节点后的链表为{}\n",
80+
.{utils.fmt.linkedList(i32, &n0)},
81+
);
7482

7583
// 访问节点
76-
var node = access(&n0, 3);
77-
std.debug.print("链表中索引 3 处的节点的值 = {}\n", .{node.?.val});
84+
const node = access(i32, &n0, 3);
85+
std.debug.print(
86+
"链表中索引 3 处的节点的值 = {}\n",
87+
.{node.?.val},
88+
);
7889

7990
// 查找节点
80-
var index = find(&n0, 2);
81-
std.debug.print("链表中值为 2 的节点的索引 = {}\n", .{index});
91+
const index = find(i32, &n0, 2);
92+
std.debug.print(
93+
"链表中值为 2 的节点的索引 = {}\n",
94+
.{index},
95+
);
8296

83-
_ = try std.io.getStdIn().reader().readByte();
84-
}
97+
std.debug.print("\n", .{});
98+
}
99+
100+
pub fn main() void {
101+
run();
102+
}
103+
104+
test "linked_list" {
105+
run();
106+
}

0 commit comments

Comments
 (0)