-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack.js
More file actions
52 lines (47 loc) · 794 Bytes
/
stack.js
File metadata and controls
52 lines (47 loc) · 794 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
49
50
51
52
class Stack {
constructor() {
/**
* @type {*[]}
*/
this.data = []
this.count = 0
}
/**
* Get stack size 获取栈的大小
* @returns {number}
*/
get size () {
return this.count
}
/**
* Building Stack of array by array
* @param {Array} array
*/
build(array) {
this.count = array.length
this.data = array
}
/**
* Add new element to the top.
* @param item
*/
push(item) {
this.data[this.count] = item
this.count++
}
/**
* The most top element of the stack is removed from the stack and returned.
* @returns {null|T}
*/
pop() {
if (this.count === 0) {
return null
}
const element = this.data.pop()
this.count--
return element
}
}
module.exports = {
Stack
}