UNPKG

615 BJavaScriptView Raw
1const LinkedList = require("./LinkedList");
2
3/**
4 * 栈
5 */
6class Stack {
7 constructor() {
8 this.linked = new LinkedList();
9 }
10
11 isEmpty() {
12 return !this.linked.head;
13 }
14
15 peek() {
16 if (this.isEmpty()) {
17 return null;
18 }
19
20 return this.linked.head.value;
21 }
22
23 push(value) {
24 this.linked.prepend(value);
25 }
26
27 pop() {
28 const removed = this.linked.deleteHead();
29 return removed ? removed.value : null;
30 }
31
32 toArrary() {
33 return this.linked.toArray().map(node => node.value);
34 }
35
36 toString(callback) {
37 return this.linked.toString(callback);
38 }
39}
40
41export default Stack;