Skip to content

Commit 4fd20d5

Browse files
committed
make HashTableSeparateChaining get and remove methods a little DRYer using do ... while instead of while loop
1 parent 2966435 commit 4fd20d5

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

chapter07/05-HashCollisionSeparateChaining.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,12 @@ function HashTableSeparateChaining(){
4141
//iterate linked list to find key/value
4242
var current = table[position].getHead();
4343

44-
while(current.next){
44+
do {
4545
if (current.element.key === key){
4646
return current.element.value;
4747
}
4848
current = current.next;
49-
}
50-
51-
//check in case first or last element
52-
if (current.element.key === key){
53-
return current.element.value;
54-
}
49+
} while(current);
5550
}
5651
return undefined;
5752
};
@@ -65,7 +60,7 @@ function HashTableSeparateChaining(){
6560
//iterate linked list to find key/value
6661
var current = table[position].getHead();
6762

68-
while(current.next){
63+
do {
6964
if (current.element.key === key){
7065
table[position].remove(current.element);
7166
if (table[position].isEmpty()){
@@ -74,16 +69,7 @@ function HashTableSeparateChaining(){
7469
return true;
7570
}
7671
current = current.next;
77-
}
78-
79-
//check in case first or last element
80-
if (current.element.key === key){
81-
table[position].remove(current.element);
82-
if (table[position].isEmpty()){
83-
table[position] = undefined;
84-
}
85-
return true;
86-
}
72+
} while(current);
8773
}
8874

8975
return false;

0 commit comments

Comments
 (0)