update zig codes for Section 'Array', 'LinkedList', 'List' and 'Stack'

This commit is contained in:
sjinzh 2023-01-12 17:08:33 +08:00
parent d75a3582b0
commit 8325ce4474

View File

@ -14,7 +14,7 @@ pub fn randomAccess(nums: []i32) i32 {
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);
@ -25,26 +25,6 @@ pub fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]
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
@ -104,7 +84,7 @@ pub fn main() !void {
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);
@ -113,14 +93,6 @@ pub fn main() !void {
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);