-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathqueue.mjs
52 lines (52 loc) · 970 Bytes
/
queue.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// src/queue.ts
var ListNode = class {
constructor(val = void 0, next = null) {
this.value = val;
this.next = next;
}
};
var Queue = class {
constructor(collection = []) {
this.lead = new ListNode();
this.tail = this.lead;
this._size = 0;
for (const item of collection)
this.push(item);
}
size() {
return this._size;
}
push(value) {
this.tail = this.tail.next = new ListNode(value);
this._size++;
}
back() {
return this.tail.value;
}
shift() {
if (!this._size)
return void 0;
const first = this.lead.next;
this.lead.next = first.next;
this._size--;
if (this._size === 0)
this.tail = this.lead;
return first.value;
}
front() {
if (!this._size)
return void 0;
return this.lead.next.value;
}
*values() {
let node = this.lead.next;
while (node) {
yield node.value;
node = node.next;
}
}
};
export {
ListNode,
Queue
};