add zig codes for Section 'Hash Map' and 'Linear Search'

This commit is contained in:
sjinzh
2023-01-13 23:23:26 +08:00
parent 71a56f94c8
commit 722fb71bdd
4 changed files with 87 additions and 2 deletions
+14
View File
@@ -18,4 +18,18 @@ pub fn ListNode(comptime T: type) type {
self.val = x;
}
};
}
// Generate a linked list with a list
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;
}
+4 -2
View File
@@ -3,5 +3,7 @@
// Author: sjinzh (sjinzh@gmail.com)
pub const PrintUtil = @import("PrintUtil.zig");
pub const ListNode = @import("ListNode.zig").ListNode;
pub const TreeNode = @import("TreeNode.zig").TreeNode;
pub const ListUtil = @import("ListNode.zig");
pub const ListNode = ListUtil.ListNode;
pub const TreeUtil = @import("TreeNode.zig");
pub const TreeNode = TreeUtil.TreeNode;