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 cffd6e2

Browse files
committed
update: remove() logic change
1 parent 3c45b0b commit cffd6e2

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

‎src/_DataStructures_/HashTable/index.js‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,29 @@ class HashTable {
164164
return null;
165165
}
166166

167-
// get all the values for the key to return
168-
// eslint-disable-next-line no-underscore-dangle
169-
const vals = this._values(index, key);
170-
const nodes = [];
167+
if (head.key === key) {
168+
let node = head;
169+
this.bucket[index] = head.next;
170+
const val = { key, value: node.value };
171+
node = null;
172+
this.size -= 1;
173+
return val;
174+
}
175+
176+
let previous = null;
177+
171178
while (head !== null) {
172-
if (head.key !== key) {
173-
nodes.push(new HashEntry({ key: head.key, value: head.value }));
179+
if (head.key === key) {
180+
let node = head;
181+
previous.next = head.next;
182+
this.size -= 1;
183+
const res = { key, value: node.value };
184+
node = null;
185+
return res;
174186
}
187+
previous = head;
175188
head = head.next;
176189
}
177-
178-
// eslint-disable-next-line no-underscore-dangle
179-
const sll = this._convertNodesToSLL(nodes);
180-
181-
this.bucket[index] = sll;
182-
// update the index with the lastest head value
183-
return { key: vals };
184190
}
185191

186192
getSize() {
@@ -197,7 +203,7 @@ class HashTable {
197203
// ht.set('hello', 'I am a new value');
198204
// // console.log(ht.bucket);
199205
// ht.set('hell', 'Bad value');
200-
// ht.set('hello', 'I am a yet another value');
206+
// // ht.set('hello', 'I am a yet another value');
201207
// ht.set('yellow', 'I am yellow');
202208

203209
// // console.log(ht.get('hello'));
@@ -208,4 +214,7 @@ class HashTable {
208214
// console.log(ht.remove('hello'));
209215
// console.log(ht.bucket);
210216

217+
// console.log(ht.remove('yellow'));
218+
// console.log(ht.bucket);
219+
211220
module.exports = HashTable;

0 commit comments

Comments
(0)

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