-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
44 lines (36 loc) · 683 Bytes
/
stack.js
File metadata and controls
44 lines (36 loc) · 683 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
class Stack {
constructor (maxSize) {
this.top = 0
this.data = []
this.maximumSize = maxSize
}
get () {
console.log('\n', [
this.top,
this.data.slice(0, this.top)
])
}
push (newItem) {
if (this.top < this.maximumSize) {
this.data[this.top] = newItem
this.top++
this.get()
} else {
console.log(`\n Stack (${this.top}) is full, can't add ${newItem}`)
}
}
pop () {
if (this.top !== 0) {
this.top--
this.get()
}
}
}
const array = new Stack(3)
array.push('Kevin')
array.push('Sally')
array.push('David')
array.push('Sonya')
array.pop()
array.push('Jessy')
array.push('Harry')