add zig codes for Section 'Binary Search' (binary_search.zig), 'Hash Search' (hashing_search.zig)

This commit is contained in:
sjinzh
2023-01-18 23:40:43 +08:00
parent 46429bcb23
commit cee7b0f4f9
4 changed files with 164 additions and 0 deletions

View File

@@ -33,4 +33,18 @@ pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list
head = head.next.?;
}
return dum.next;
}
// Generate a linked list with an array
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;
}