Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
949 bytes
|
Line | |
---|
1 | class Node {
|
---|
2 | /// value;
|
---|
3 | /// next;
|
---|
4 |
|
---|
5 | constructor(value) {
|
---|
6 | this.value = value;
|
---|
7 |
|
---|
8 | // TODO: Remove this when targeting Node.js 12.
|
---|
9 | this.next = undefined;
|
---|
10 | }
|
---|
11 | }
|
---|
12 |
|
---|
13 | class Queue {
|
---|
14 | // TODO: Use private class fields when targeting Node.js 12.
|
---|
15 | // #_head;
|
---|
16 | // #_tail;
|
---|
17 | // #_size;
|
---|
18 |
|
---|
19 | constructor() {
|
---|
20 | this.clear();
|
---|
21 | }
|
---|
22 |
|
---|
23 | enqueue(value) {
|
---|
24 | const node = new Node(value);
|
---|
25 |
|
---|
26 | if (this._head) {
|
---|
27 | this._tail.next = node;
|
---|
28 | this._tail = node;
|
---|
29 | } else {
|
---|
30 | this._head = node;
|
---|
31 | this._tail = node;
|
---|
32 | }
|
---|
33 |
|
---|
34 | this._size++;
|
---|
35 | }
|
---|
36 |
|
---|
37 | dequeue() {
|
---|
38 | const current = this._head;
|
---|
39 | if (!current) {
|
---|
40 | return;
|
---|
41 | }
|
---|
42 |
|
---|
43 | this._head = this._head.next;
|
---|
44 | this._size--;
|
---|
45 | return current.value;
|
---|
46 | }
|
---|
47 |
|
---|
48 | clear() {
|
---|
49 | this._head = undefined;
|
---|
50 | this._tail = undefined;
|
---|
51 | this._size = 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | get size() {
|
---|
55 | return this._size;
|
---|
56 | }
|
---|
57 |
|
---|
58 | * [Symbol.iterator]() {
|
---|
59 | let current = this._head;
|
---|
60 |
|
---|
61 | while (current) {
|
---|
62 | yield current.value;
|
---|
63 | current = current.next;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | module.exports = Queue;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.