Skip to content

Commit bd9f47e

Browse files
committed
Fix 7-current implementation
1 parent cf42fb6 commit bd9f47e

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

JavaScript/7-current.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class UnrolledQueue {
6969
if (!this.#current.enqueue(item)) {
7070
if (this.#current === this.#head) {
7171
const node = this.#createNode();
72-
node.next = this.#head;
73-
this.#head = node;
7472
this.#current.next = node;
73+
this.#current = node;
74+
this.#head = node;
7575
} else {
7676
this.#current = this.#current.next;
7777
}
@@ -82,15 +82,15 @@ class UnrolledQueue {
8282

8383
dequeue() {
8484
if (this.#length === 0) return null;
85-
const tail = this.#tail;
86-
if (tail.length === 0) {
87-
this.#tail = tail.next;
88-
tail.reset();
89-
this.#head.next = tail;
90-
this.#head = tail;
91-
}
92-
const item = this.#tail.dequeue();
85+
const node = this.#tail;
86+
const item = node.dequeue();
9387
this.#length--;
88+
if (node.length === 0 && node !== this.#current) {
89+
this.#tail = node.next;
90+
node.reset();
91+
this.#head.next = node;
92+
this.#head = node;
93+
}
9494
return item;
9595
}
9696
}

0 commit comments

Comments
 (0)