-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-stack.js
More file actions
47 lines (41 loc) · 828 Bytes
/
array-stack.js
File metadata and controls
47 lines (41 loc) · 828 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
class Stack{
constructor(){
this.items = []
this.size = 0
}
push(item){
this.items[this.size++] = item
return item
}
pop(){
if(this.isEmpty()) return null
const removed = this.items[this.size - 1]
delete this.items[this.size - 1]
this.size--
return removed
}
top(){
if(this.isEmpty()) return null
return this.items[this.size - 1]
}
isEmpty(){
this.size ? false : true
}
getStack(){
let stack = []
for(let i=0;i<this.size;i++){
stack[i] = (this.items[i])
}
return stack;
}
}
const stack = new Stack()
console.log(stack.push(1))
console.log(stack.push(2))
console.log(stack.push(3))
console.log("---")
console.log(stack.getStack())
console.log("---")
console.log(stack.pop())
console.log(stack.getStack())
console.log(stack.top())