-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathHashTable.ts
More file actions
105 lines (91 loc) · 2.35 KB
/
Copy pathHashTable.ts
File metadata and controls
105 lines (91 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
* @description: 哈希表数据结构(整数键) - TypeScript实现
*/
const TABLE_SIZE = 10;
interface HashNode {
key: number;
value: number;
next: HashNode | null;
}
class HashTable {
private table: (HashNode | null)[];
constructor() {
this.table = new Array(TABLE_SIZE).fill(null);
}
// 多项式哈希函数
private hash(key: number): number {
let hash = 0;
const base = 31;
let tempKey = key;
while (tempKey > 0) {
hash = (hash * base + tempKey % 10) % TABLE_SIZE;
tempKey = Math.floor(tempKey / 10);
}
return hash;
}
insert(key: number, value: number): void {
const index = this.hash(key);
const newNode: HashNode = { key, value, next: this.table[index] };
this.table[index] = newNode;
}
search(key: number): number {
const index = this.hash(key);
let current: HashNode | null = this.table[index];
while (current !== null) {
if (current.key === key) {
return current.value;
}
current = current.next;
}
return -1;
}
delete(key: number): void {
const index = this.hash(key);
let current: HashNode | null = this.table[index];
let prev: HashNode | null = null;
while (current !== null) {
if (current.key === key) {
if (prev === null) {
this.table[index] = current.next;
} else {
prev.next = current.next;
}
return;
}
prev = current;
current = current.next;
}
}
printTable(): void {
for (let i = 0; i < TABLE_SIZE; i++) {
const node: HashNode | null = this.table[i];
if (node !== null) {
let output = `Index ${i}: `;
let current: HashNode | null = node;
while (current !== null) {
output += `[${current.key}:${current.value}] `;
current = current.next;
}
console.log(output.trim());
}
}
}
}
const hashTable = new HashTable();
hashTable.insert(1, 100);
hashTable.insert(2, 200);
hashTable.insert(3, 300);
console.log("Search key 2:", hashTable.search(2));
hashTable.delete(2);
console.log("After deleting key 2:");
hashTable.printTable();
/*
* 输出结果:
* Search key 2: 200
* After deleting key 2:
* Index 1: [1:100]
* Index 3: [3:300]
*/