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 bff5589

Browse files
Refactoring insert method
1 parent 2816a1e commit bff5589

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

‎hashTable.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ HashTable.prototype.hash = function(key) {
2121
HashTable.prototype.insert = function(key, value) {
2222
let index = this.hash(key);
2323
if (!this.buckets[index]) this.buckets[index] = new HashNode(key, value);
24+
else if (this.buckets[index].key === key) {
25+
this.buckets[index].value = value;
26+
}
2427
else {
2528
let currentNode = this.buckets[index];
2629
while (currentNode.next) {
30+
if (currentNode.next.key === key) {
31+
currentNode.next.value = value;
32+
return;
33+
}
2734
currentNode = currentNode.next;
2835
}
2936
currentNode.next = new HashNode(key, value);
@@ -35,5 +42,8 @@ let myHT = new HashTable(30);
3542
myHT.insert('Dean', 'dean@gmail.com');
3643
myHT.insert('Megan', 'megan@gmail.com');
3744
myHT.insert('Dane', 'dane@yahoo.com');
45+
myHT.insert('Dean', 'deanmachine@gmail.com');
46+
myHT.insert('Megan', 'megansmith@gmail.com');
47+
myHT.insert('Dane', 'dane1010@outlook.com');
3848

3949
console.log(myHT.buckets);

0 commit comments

Comments
(0)

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