Update linear_search and hashing_search.

This commit is contained in:
Yudong Jin
2023-02-04 23:49:37 +08:00
parent 62114ce79a
commit f14e3e4c57
22 changed files with 95 additions and 92 deletions

View File

@ -6,7 +6,7 @@ const std = @import("std");
const inc = @import("include");
// 哈希查找(数组)
fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
fn hashingSearchArray(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
// 哈希表的 key: 目标元素value: 索引
// 若哈希表中无此 key ,返回 -1
if (map.getKey(target) == null) return -1;
@ -14,7 +14,7 @@ fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
}
// 哈希查找(数组)
fn hashingSearch1(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
fn hashingSearchLinkedList(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
// 哈希表的 key: 目标结点值value: 结点对象
// 若哈希表中无此 key ,返回 null
if (map.getKey(target) == null) return null;
@ -33,7 +33,7 @@ pub fn main() !void {
for (nums) |num, i| {
try map.put(num, @intCast(i32, i)); // key: 元素value: 索引
}
var index = hashingSearch(i32, map, target);
var index = hashingSearchArray(i32, map, target);
std.debug.print("目标元素 3 的索引 = {}\n", .{index});
// 哈希查找(链表)
@ -48,7 +48,7 @@ pub fn main() !void {
try map1.put(head.?.val, head.?);
head = head.?.next;
}
var node = hashingSearch1(i32, map1, target);
var node = hashingSearchLinkedList(i32, map1, target);
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
try inc.PrintUtil.printLinkedList(i32, node);