-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
49 lines (38 loc) · 753 Bytes
/
stack.js
File metadata and controls
49 lines (38 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
38
39
40
41
42
43
44
45
46
47
48
49
class Node {
constructor(value, next) {
this.value = value;
this.next = next;
}
}
class Stack {
constructor() {
this.top = null;
this.bottom = null;
this.length = 0;
}
push(value) {
let node = new Node(value);
if (!this.top) {
this.top = node;
this.bottom = node;
} else {
const temp = this.top;
this.top = node;
this.top.next = temp;
}
return ++this.length;
}
pop() {
if (!this.top) return null;
const node = this.top;
if (this.top === this.bottom) this.top = this.bottom = null;
else this.top = this.top.next;
this.length--;
return node.value;
}
size() {
return this.length;
}
}
exports.Node = Node;
exports.Stack = Stack;