-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
35 lines (34 loc) · 910 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
const stackPrototype = {
push: function (item) {
if (this.quantity < this.maxSize) {
this.storage[this.currentIndex] = item
this.currentIndex++
this.quantity++
}
},
pop: function () {
if (this.quantity > 0) {
this.currentIndex--
this.quantity--
let pop = this.storage[this.currentIndex]
delete this.storage[this.currentIndex]
return pop
}
},
isEmpty: function() {
return this.quantity === 0
},
isFull: function () {
return this.quantity === this.maxSize
},
peak: function() {
if (this.quantity > 0) {return this.storage[this.currentIndex-1]}
}
}
function createStack(maxSize=1) {
let stack = Object.create(stackPrototype)
stack.storage = {}
stack.quantity = 0
stack.currentIndex = 1
return stack
}