Skip to content

Commit ea68789

Browse files
committed
refactor: optimized internal structure of the queue
1 parent 52205db commit ea68789

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

round-linked-queue.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class RoundLinkedQueue {
44
constructor(maxLength) {
55
this._maxLength = maxLength;
66
this._length = 0;
7-
this._root = 0;
87
this._first = null;
98
this._last = null;
109
}
@@ -32,24 +31,18 @@ class RoundLinkedQueue {
3231
};
3332

3433
if (this.length < this.maxLength) {
35-
if (!this._root) {
36-
this._root = node;
34+
if (!this._first) {
3735
this._first = node;
38-
this._last = node;
39-
} else {
40-
const previousLast = this._last;
41-
previousLast.next = node;
42-
4336
this._last = node;
4437
}
4538

4639
this._length += 1;
4740
} else {
48-
this._root = this._root.next;
49-
this._last.next = node;
50-
this._first = this._root;
51-
this._last = node;
41+
this._first = this._first.next;
5242
}
43+
44+
this._last.next = node;
45+
this._last = node;
5346
}
5447
}
5548

0 commit comments

Comments
 (0)