We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a14c432 commit 55541a2Copy full SHA for 55541a2
hash/chaining.ts
@@ -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