Skip to content

Commit 55541a2

Browse files
committed
Hash - chaining
1 parent a14c432 commit 55541a2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

hash/chaining.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const TABLE_SIZE = 10;
2+
3+
const hashTable = Array(TABLE_SIZE).fill(null);
4+
5+
function hashFunction(key: number) {
6+
return key % TABLE_SIZE;
7+
}
8+
9+
function hashAdd(item: number) {
10+
const hashValue = hashFunction(item);
11+
const node = hashTable[hashValue];
12+
let nodeBefore;
13+
let ptr = node;
14+
while (ptr) {
15+
if (ptr.value === item) {
16+
console.log("키가 중복됐습니다.");
17+
throw new Error("Duplicated");
18+
}
19+
20+
nodeBefore = ptr;
21+
ptr = ptr.next;
22+
}
23+
24+
if (node === null) {
25+
hashTable[hashValue] = { value: item, next: null };
26+
} else {
27+
nodeBefore.next = { value: item, next: null };
28+
}
29+
}
30+
31+
export {};

0 commit comments

Comments
 (0)