-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
51 lines (41 loc) · 1.03 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
// Linked List
// Pop / Push (add, remove) at one end in stack
// Linked List
const { LinkedList } = require('../linked-list/linked-list');
function Stack() {
this.stack = new LinkedList();
}
// Stack is empty -- isEmpty
// Find element at top -- peek
// Push -- push
// Pop --- pop
Stack.prototype.isEmpty = function() {
return this.stack.size === 0;
}
// Push and Pop at the same end
// Linked List - we can access the first index in constant time
// Push 1 ---> 1
// Push 2 ---> 2 --- 1
// Push 3 ---> 3 --- 2 --- 1
Stack.prototype.push = function(value) {
this.stack.addAtIndex(0, value);
// console.log(this.stack);
}
// Stack ---> 3 --- 2 --- 1
// Pop ---> 2 --- 1
// Pop ---> 1
Stack.prototype.pop = function() {
if (this.isEmpty()) {
return null;
}
const node = this.stack.deleteAtIndex(0);
return node.value;
}
Stack.prototype.peek = function() {
if (this.isEmpty()) {
return null;
}
const node = this.stack.getNodeAtIndex(0);
return node.value;
}
exports.Stack = Stack;