Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2816a1e

Browse files
Insert hash method
1 parent 4472ecc commit 2816a1e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

‎hashTable.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ HashTable.prototype.hash = function(key) {
1818
return bucket;
1919
}
2020

21+
HashTable.prototype.insert = function(key, value) {
22+
let index = this.hash(key);
23+
if (!this.buckets[index]) this.buckets[index] = new HashNode(key, value);
24+
else {
25+
let currentNode = this.buckets[index];
26+
while (currentNode.next) {
27+
currentNode = currentNode.next;
28+
}
29+
currentNode.next = new HashNode(key, value);
30+
}
31+
}
32+
2133
let myHT = new HashTable(30);
2234

23-
console.log(myHT.hash('Becca'));
35+
myHT.insert('Dean', 'dean@gmail.com');
36+
myHT.insert('Megan', 'megan@gmail.com');
37+
myHT.insert('Dane', 'dane@yahoo.com');
38+
39+
console.log(myHT.buckets);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /