-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathQueue.js
99 lines (84 loc) · 2.2 KB
/
Queue.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 队列Queue
*
* 队列是一种先进先出(first in first out, FIFO)的线性表。它只允许在表的一端进行插入,而在另一端删除元素。
* 允许插入的一端叫队尾(rear),允许删除的一端叫队头(front)。
*/
// 链队列
export default class Queue {
constructor() {
this.rear = this.front = null;
this.size = 0;
}
isEmpty() {
return this.rear === null;
}
clear() {
this.rear = this.front = null;
this.size = 0;
}
getHead() {
return this.front ? this.front.data : null;
}
enQueue(elem) {
if (this.front === null) {
this.rear = this.front = { data: elem, next: null };
} else {
let p = { data: elem, next: null };
this.rear.next = p;
this.rear = p;
}
this.size++;
}
deQueue() {
if (this.front) {
let elem = this.front.data;
this.front = this.front.next;
if (this.front === null) {
this.rear = null;
}
this.size--;
return elem;
} else {
return null;
}
}
forEach(iterator) {
if (typeof iterator !== 'function')
throw new Error('iterator should be function');
let current = this.front;
while (current) {
if (iterator(current.data)) break;
current = current.next;
}
}
*[Symbol.iterator]() {
let current = this.front;
while (current) {
yield current.data;
current = current.next;
}
}
peekAt(index = 0) {
if (index < this.size) {
let current = this.front;
for (let i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
return -1;
}
toString() {
if (this.front === null) {
return null;
}
let arr = [];
let current = this.front;
for (let i = 0, len = this.size; i < len; i++) {
arr[i] = current.data;
current = current.next;
}
return arr;
}
}