-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.js
46 lines (37 loc) · 887 Bytes
/
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
// STACK
// Time Complexity
// push - O(1)
// pop - O(1)
// peek - O(1)
// Space Complexity
// - O(n)
export default class Stack {
constructor(printAllSteps = false) {
this.stack = []
this.printAllSteps = printAllSteps
}
push(ele) {
this.printAllSteps && console.log('pushing...', ele)
this.stack.push(ele)
}
pop() {
this.printAllSteps &&
console.log('popping...', this.stack[this.stack.length - 1])
return this.stack.pop()
}
peek() {
let peekElement = this.stack[this.stack.length - 1]
this.printAllSteps && console.log('peeking...', peekElement)
return peekElement
}
isEmpty() {
return this.stack.length === 0
}
size() {
this.printAllSteps && console.log('getting size...', this.stack.length)
return this.stack.length
}
print() {
console.log('stack --->', this.stack)
}
}