UNPKG

596 BJavaScriptView Raw
1import LinkedList from "./LinkedList";
2
3/**
4 * 队列(排队)
5 */
6class Queue {
7 constructor() {
8 this.linkedList = new LinkedList();
9 }
10
11 isEmpty() {
12 return !this.linkedList.head;
13 }
14
15 peek() {
16 if (!this.linkedList.head) {
17 return null;
18 }
19 return this.linkedList.head.value;
20 }
21
22 //入列
23 enqueue() {
24 this.linkedList.append(value);
25 }
26
27 //出列
28 dequeue() {
29 const removed = this.linkedList.deleteHead();
30 return removed ? removed.value : null;
31 }
32
33 toString(callback) {
34 return this.linkedList.toString(callback);
35 }
36}
37
38export default Queue;