forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add zig codes for Section 'Array', 'LinkedList' and 'List'
- Loading branch information
1 parent
368bf0d
commit a1579f6
Showing
6 changed files
with
589 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// File: array.zig | ||
// Created Time: 2023-01-07 | ||
// Author: sjinzh ([email protected]) | ||
|
||
const std = @import("std"); | ||
const inc = @import("include"); | ||
|
||
// 随机返回一个数组元素 | ||
pub fn randomAccess(nums: []i32) i32 { | ||
// 在区间 [0, nums.len) 中随机抽取一个整数 | ||
var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len); | ||
// 获取并返回随机元素 | ||
var randomNum = nums[randomIndex]; | ||
return randomNum; | ||
} | ||
|
||
// 扩展数组长度(运行时方法) | ||
pub fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 { | ||
// 初始化一个扩展长度后的数组 | ||
var res = try mem_allocator.alloc(i32, nums.len + enlarge); | ||
std.mem.set(i32, res, 0); | ||
// 将原数组中的所有元素复制到新数组 | ||
std.mem.copy(i32, res, nums); | ||
// 返回扩展后的新数组 | ||
return res; | ||
} | ||
|
||
// 扩展数组长度(编译期方法A:初始化新数组并进行元素拷贝) | ||
pub fn extendComptimeA(comptime nums: anytype, comptime enlarge: i32) [nums.len + enlarge]i32 { | ||
// 初始化一个扩展长度后的数组 | ||
var res = [_]i32{0} ** (nums.len + enlarge); | ||
// 将原数组中的所有元素复制到新数组 | ||
for (nums) |num, i| { | ||
res[i] = num; | ||
} | ||
// 返回扩展后的新数组 | ||
return res; | ||
} | ||
|
||
// 扩展数组长度(编译期方法B: 通过数组拼接运算符“++”) | ||
pub fn extendComptimeB(comptime nums: anytype, comptime enlarge: i32) [nums.len + enlarge]i32 { | ||
// 数组拼接操作 | ||
var res = nums ++ [_]i32{0} ** enlarge; | ||
// 返回拼接后的新数组 | ||
return res; | ||
} | ||
|
||
// 在数组的索引 index 处插入元素 num | ||
pub fn insert(nums: []i32, num: i32, index: usize) void { | ||
// 把索引 index 以及之后的所有元素向后移动一位 | ||
var i = nums.len - 1; | ||
while (i > index) : (i -= 1) { | ||
nums[i] = nums[i - 1]; | ||
} | ||
// 将 num 赋给 index 处元素 | ||
nums[index] = num; | ||
} | ||
|
||
// 删除索引 index 处元素 | ||
pub fn remove(nums: []i32, index: usize) void { | ||
// 把索引 index 之后的所有元素向前移动一位 | ||
var i = index; | ||
while (i < nums.len - 1) : (i += 1) { | ||
nums[i] = nums[i + 1]; | ||
} | ||
} | ||
|
||
// 遍历数组 | ||
pub fn traverse(nums: []i32) void { | ||
var count: i32 = 0; | ||
// 通过索引遍历数组 | ||
var i: i32 = 0; | ||
while (i < nums.len) : (i += 1) { | ||
count += 1; | ||
} | ||
count = 0; | ||
// 直接遍历数组 | ||
for (nums) |_| { | ||
count += 1; | ||
} | ||
} | ||
|
||
// 在数组中查找指定元素 | ||
pub fn find(nums: []i32, target: i32) i32 { | ||
for (nums) |num, i| { | ||
if (num == target) return @intCast(i32, i); | ||
} | ||
return -1; | ||
} | ||
|
||
// Driver Code | ||
pub fn main() !void { | ||
// 查看本地CPU架构和操作系统信息 | ||
var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); | ||
std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); | ||
|
||
// 初始化数组 | ||
const size: i32 = 5; | ||
var arr = [_]i32{0} ** size; | ||
std.debug.print("数组 arr = ", .{}); | ||
inc.PrintUtil.printArray(i32, &arr); | ||
|
||
var array = [_]i32{ 1, 3, 2, 5, 4 }; | ||
std.debug.print("\n数组 nums = ", .{}); | ||
inc.PrintUtil.printArray(i32, &array); | ||
|
||
// 随机访问 | ||
var randomNum = randomAccess(&array); | ||
std.debug.print("\n在 nums 中获取随机元素 {}", .{randomNum}); | ||
|
||
// 长度扩展(运行时方法) | ||
var known_at_runtime_zero: usize = 0; | ||
var nums: []i32 = array[known_at_runtime_zero..array.len]; | ||
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer mem_arena.deinit(); | ||
const mem_allocator = mem_arena.allocator(); | ||
nums = try extend(mem_allocator, nums, 3); | ||
std.debug.print("\n将数组长度扩展至 8 ,得到 nums = ", .{}); | ||
inc.PrintUtil.printArray(i32, nums); | ||
// { | ||
// // 长度扩展(编译期方法) | ||
// comptime var array_comptime = [_]i32{ 1, 3, 2, 5, 4 }; | ||
// var nums_comptime = extendComptimeA(array_comptime, 3); | ||
// // var nums_comptime = extendComptimeB(array_comptime, 3); | ||
// std.debug.print("\n将数组长度扩展至 8 ,得到 nums_comptime = ", .{}); | ||
// inc.PrintUtil.printArray(i32, &nums_comptime); | ||
// } | ||
|
||
// 插入元素 | ||
insert(nums, 6, 3); | ||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 nums = ", .{}); | ||
inc.PrintUtil.printArray(i32, nums); | ||
|
||
// 删除元素 | ||
remove(nums, 2); | ||
std.debug.print("\n删除索引 2 处的元素,得到 nums = ", .{}); | ||
inc.PrintUtil.printArray(i32, nums); | ||
|
||
// 遍历数组 | ||
traverse(nums); | ||
|
||
// 查找元素 | ||
var index = find(nums, 3); | ||
std.debug.print("\n在 nums_ext 中查找元素 3 ,得到索引 = {}\n", .{index}); | ||
|
||
const getchar = try std.io.getStdIn().reader().readByte(); | ||
_ = getchar; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// File: linked_list.zig | ||
// Created Time: 2023-01-07 | ||
// Author: sjinzh ([email protected]) | ||
|
||
const std = @import("std"); | ||
const inc = @import("include"); | ||
|
||
// 在链表的结点 n0 之后插入结点 P | ||
pub fn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void { | ||
var n1 = n0.?.next; | ||
n0.?.next = P; | ||
P.?.next = n1; | ||
} | ||
|
||
// 删除链表的结点 n0 之后的首个结点 | ||
pub fn remove(n0: ?*inc.ListNode(i32)) void { | ||
if (n0.?.next == null) return; | ||
// n0 -> P -> n1 | ||
var P = n0.?.next; | ||
var n1 = P.?.next; | ||
n0.?.next = n1; | ||
} | ||
|
||
// 访问链表中索引为 index 的结点 | ||
pub fn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) { | ||
var head = node; | ||
var i: i32 = 0; | ||
while (i < index) : (i += 1) { | ||
head = head.?.next; | ||
if (head == null) return null; | ||
} | ||
return head; | ||
} | ||
|
||
// 在链表中查找值为 target 的首个结点 | ||
pub fn find(node: ?*inc.ListNode(i32), target: i32) i32 { | ||
var head = node; | ||
var index: i32 = 0; | ||
while (head != null) { | ||
if (head.?.val == target) return index; | ||
head = head.?.next; | ||
index += 1; | ||
} | ||
return -1; | ||
} | ||
|
||
// Driver Code | ||
pub fn main() !void { | ||
// 查看本地CPU架构和操作系统信息 | ||
var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); | ||
std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); | ||
|
||
// 初始化链表 | ||
// 初始化各个结点 | ||
var n0 = inc.ListNode(i32){.val = 1}; | ||
var n1 = inc.ListNode(i32){.val = 3}; | ||
var n2 = inc.ListNode(i32){.val = 2}; | ||
var n3 = inc.ListNode(i32){.val = 5}; | ||
var n4 = inc.ListNode(i32){.val = 4}; | ||
// 构建引用指向 | ||
n0.next = &n1; | ||
n1.next = &n2; | ||
n2.next = &n3; | ||
n3.next = &n4; | ||
std.debug.print("初始化的链表为", .{}); | ||
try inc.PrintUtil.printLinkedList(i32, &n0); | ||
|
||
// 插入结点 | ||
var tmp = inc.ListNode(i32){.val = 0}; | ||
insert(&n0, &tmp); | ||
std.debug.print("插入结点后的链表为", .{}); | ||
try inc.PrintUtil.printLinkedList(i32, &n0); | ||
|
||
// 删除结点 | ||
remove(&n0); | ||
std.debug.print("删除结点后的链表为", .{}); | ||
try inc.PrintUtil.printLinkedList(i32, &n0); | ||
|
||
// 访问结点 | ||
var node = access(&n0, 3); | ||
std.debug.print("链表中索引 3 处的结点的值 = {}\n", .{node.?.val}); | ||
|
||
// 查找结点 | ||
var index = find(&n0, 2); | ||
std.debug.print("链表中值为 2 的结点的索引 = {}\n", .{index}); | ||
|
||
const getchar = try std.io.getStdIn().reader().readByte(); | ||
_ = getchar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// File: list.zig | ||
// Created Time: 2023-01-07 | ||
// Author: sjinzh ([email protected]) | ||
|
||
const std = @import("std"); | ||
const inc = @import("include"); | ||
|
||
// Driver Code | ||
pub fn main() !void { | ||
// 查看本地CPU架构和操作系统信息 | ||
var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); | ||
std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); | ||
|
||
// 初始化列表 | ||
var list = std.ArrayList(i32).init(std.heap.page_allocator); | ||
// 延迟释放内存 | ||
defer list.deinit(); | ||
try list.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 }); | ||
std.debug.print("列表 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 访问元素 | ||
var num = list.items[1]; | ||
std.debug.print("\n访问索引 1 处的元素,得到 num = {}", .{num}); | ||
|
||
// 更新元素 | ||
list.items[1] = 0; | ||
std.debug.print("\n将索引 1 处的元素更新为 0 ,得到 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 清空列表 | ||
list.clearRetainingCapacity(); | ||
std.debug.print("\n清空列表后 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 尾部添加元素 | ||
try list.append(1); | ||
try list.append(3); | ||
try list.append(2); | ||
try list.append(5); | ||
try list.append(4); | ||
std.debug.print("\n添加元素后 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 中间插入元素 | ||
try list.insert(3, 6); | ||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 删除元素 | ||
var value = list.orderedRemove(3); | ||
_ = value; | ||
std.debug.print("\n删除索引 3 处的元素,得到 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 通过索引遍历列表 | ||
var count: i32 = 0; | ||
var i: i32 = 0; | ||
while (i < list.items.len) : (i += 1) { | ||
count += 1; | ||
} | ||
|
||
// 直接遍历列表元素 | ||
count = 0; | ||
for (list.items) |_| { | ||
count += 1; | ||
} | ||
|
||
// 拼接两个列表 | ||
var list1 = std.ArrayList(i32).init(std.heap.page_allocator); | ||
defer list1.deinit(); | ||
try list1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 }); | ||
try list.insertSlice(list.items.len, list1.items); | ||
std.debug.print("\n将列表 list1 拼接到 list 之后,得到 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
// 排序列表 | ||
std.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32)); | ||
std.debug.print("\n排序列表后 list = ", .{}); | ||
inc.PrintUtil.printList(i32, list); | ||
|
||
const getchar = try std.io.getStdIn().reader().readByte(); | ||
_ = getchar; | ||
} | ||
|
Oops, something went wrong.