-
Notifications
You must be signed in to change notification settings - Fork 117
/
Stack.js
55 lines (41 loc) · 1.06 KB
/
Stack.js
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
50
51
52
53
54
55
// Stack
// Implement the following methods on the Stack class:
// push - takes in a node and puts it at the top of the stack.
// Should return the new size of the stack.
// pop - removes the node at the top of the stack and returns the value of that node.
const StackNode = require('./Stack-node');
class Stack {
constructor() {
this.first = null;
this.last = null;
this.size = 0;
}
push(data) {
this.first = new StackNode(data, this.first);
if (!this.size) this.last = this.first;
this.size++;
return this.size;
}
pop() {
if (!this.first) return null;
const removedNode = this.first;
this.first = removedNode.next;
this.size--;
if (!this.size) this.last = null;
return removedNode.data;
}
}
if (module.parent) {
module.exports = Stack;
} else {
const stack = new Stack();
stack.push(10);
stack.push(100);
stack.push(1000);
console.log(stack.pop()); // 1000
console.log(stack.size); // 2
stack.pop();
stack.pop();
console.log(stack.size); // 0
console.log(stack.pop()); // null
}