-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
37 lines (35 loc) · 753 Bytes
/
stack.js
File metadata and controls
37 lines (35 loc) · 753 Bytes
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
class StackNode {
constructor(_val, _next = null) {
this.val = _val;
this.next = _next;
}
}
class Stack { // first
constructor() {
this.top = null;
}
getContainer(me) {
let returning = me;
const goBackwards = node => {
if(node === null) {
return;
}
goBackwards(node.next);
returning = returning.contents[node.val];
}
goBackwards(this.top);
return returning;
}
push(val) {
this.top = new StackNode(val, this.top);
}
pop() {
if(this.top !== null) {
this.top = this.top.next;
}
}
clear() {
this.top = null;
}
}
module.exports = Stack;