-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.js
53 lines (48 loc) · 1.78 KB
/
hash_table.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
function HashTable(size) {
this.table = Array(size);
this.sizeOfTable = size;
}
function HashNode(key, value, next) {
this.key = key;
this.value = value;
this.next = next || null;
}
HashTable.prototype.hash = function(key) {
var total = 0;
for(var i = 0; i < key.length; i++) {
total += key.charCodeAt(i);
}
//return the index number within sizeOfTable
return (total % this.sizeOfTable);
}
HashTable.prototype.insert = function(key, value) {
var index = this.hash(key);
if(!this.table[index]) {
this.table[index] = new HashNode(key, value);
}else if(this.table[index].key === key) {
//if node with same key exists, than update that node
this.table[index].value = value;
}else {
//if node exists and they have different key but same index, than add new node as a linked list
var currentNode = this.table[index];
while(currentNode.next) {
currentNode = currentNode.next;
//if there are linked nodes on particular index and want to update specific node than traverse through those nodes and //update the value
if(currentNode.next.key === key) {
currentNode.next.value = value;
//after updating the value just return without creating any new node at the end of the linked list
return;
}
}
//after travrsing to the last item of the linked list of same index insert new node
currentNode.next = new HashNode(key, value);
}
}
var ht = new HashTable(20);
ht.insert("email", "[email protected]");
ht.insert("email", "[email protected]");
ht.insert("email1", "[email protected]");
ht.insert("email2", "[email protected]");
ht.insert("Dean", "[email protected]");
ht.insert("Dane", "[email protected]");
console.log(ht.table);