-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
48 lines (39 loc) · 764 Bytes
/
stack.js
File metadata and controls
48 lines (39 loc) · 764 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
class Stack {
constructor() {
this.arr = []
this.index = 0
}
push(value) {
this.arr[this.index++] = value
}
pop() {
if (this.index <= 0) return null
const result = this.arr[this.index--]
return result
}
}
let stack = new Stack()
stack.push('11')
stack.push('22')
stack.push('33')
stack.push('44')
stack.push('55')
console.log('stack:', stack)
stack.pop()
stack.pop()
stack.pop()
stack.pop()
console.log('stack:', stack)
let stack2 = new Array()
stack2.push('11')
stack2.push('22')
stack2.push('33')
stack2.push('44')
stack2.push('55')
console.log('stack2:', stack2)
stack2.pop()
stack2.pop()
stack2.pop()
let reversed = stack2.slice().reverse()
console.log('stack2 reversed: ', reversed)
console.log('stack2:', stack2)