-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash-table-with-linked-list.js
96 lines (71 loc) · 2.13 KB
/
hash-table-with-linked-list.js
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
const LinkedList = require("../linked-list/linked-list");
class HashTable {
constructor() {
const limit = (this.limit = 5);
this.storage = new Array(limit);
}
hash(string) {
let hash = 0;
for (let i = 0; i < string.length; i++) {
hash += string.charCodeAt(i);
}
return hash % this.limit;
}
add(key, value) {
const position = this.hash(key);
if (!this.storage[position]) {
this.storage[position] = new LinkedList();
}
if (this.updateBucket(this.storage[position].head, key, value)) return;
return this.storage[position].append([key, value]);
}
updateBucket(bucket, key, value) {
if (!bucket) return;
if (bucket.data[0] === key) {
return (bucket.data[1] = value);
}
return this.updateBucket(bucket.next, key, value);
}
removeFromBucket(bucket, key) {
if (!bucket.data) return;
if (bucket.next.data[0] === key) {
return (bucket.next = bucket.next.next);
}
return this.removeFromBucket(bucket.next, key);
}
lookup(key) {
const position = this.hash(key);
if (!this.storage[position]) return;
let current = this.storage[position].head;
while (current.next) {
if (current.data[0] === key) {
break;
}
current = current.next;
}
return current.data[0] === key && current.data;
}
remove(key) {
const position = this.hash(key);
if (!this.storage[position]) return;
const bucket = this.storage[position];
if (bucket.head.data[0] === key) {
bucket.head = bucket.head.next;
return;
}
this.removeFromBucket(bucket.head, key);
}
}
// const myHashTable = new HashTable();
// myHashTable.add("apelido", "nevinha");
// myHashTable.add("nome", "Clóvis Neto");
// myHashTable.add("idade", "24");
// myHashTable.add("github", "clovisdasilvaneto");
// myHashTable.add("idaade", "aaaa");
// myHashTable.add("idaaade", "aaaa");
// myHashTable.add("idaaaade", "aaaa");
// myHashTable.add("idaaaaade", "aaaa");
// myHashTable.add("idaaaaaade", "aaaa");
// myHashTable.add("github", "nevinha");
// myHashTable.remove("github");
// console.log(myHashTable.storage[3]);