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
| class Node { constructor(data = -1, next = null) { this.data = data this.next = next } } class Stack { constructor() { this.top = null this.elems = 0 }
push(elem) { this.top = new Node(elem, this.top) this.elems++ }
pop() { if (this.elems == 0) { console.log("stack is empty") return } let current = this.top let elem = current.data this.top = this.top.next this.elems-- current = null return elem }
peekTop() { return this.top.data } }
|