UNPKG

991 BJavaScriptView Raw
1import { Collection } from "../Collection";
2import LinkList from "../linklist/LinkList";
3export class Stack extends Collection {
4 constructor() {
5 super();
6 this.linkList = new LinkList();
7 }
8 push(node) {
9 return this.linkList.append(node);
10 }
11 pop() {
12 const node = this.linkList.pop();
13 if (node) {
14 return node.Value;
15 }
16 return null;
17 }
18 peek() {
19 if (!this.linkList.getTailNode()) {
20 return null;
21 }
22 return this.linkList.getTailNode().Value;
23 }
24 isEmpty() {
25 return !this.linkList.getTailNode();
26 }
27 toString() {
28 return this.linkList.toString();
29 }
30 __iterate(fn) {
31 let temp = this.linkList.getHeadNode(), index = 0;
32 while (temp) {
33 fn(temp.Value, index);
34 index++;
35 temp = temp.Next;
36 }
37 }
38 toArray() {
39 return super.toArray().reverse();
40 }
41}
42export default Stack;