栈 Stack
温故知新,可跳转直接概念 - 栈 Stack
栈的实现
参考代码
js
class Stack {
constructor(size = 0, storage = {}) {
this.size = size
this.storage = storage
}
push(data) {
let size = ++this.size
this.storage[size] = data
}
pop() {
let size = this.size
let deletedData = null
if (size) {
deletedData = this.storage[size]
delete this.storage[size]
this.size--
return deletedData
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
js
function Stack() {
this._size = 0
this._storage = {}
}
Stack.prototype.push = function(data) {
let size = ++this._size
this._storage[size] = data
};
Stack.prototype.pop = function() {
let size = this._size
let deletedData = null
if (size) {
deletedData = this._storage[size]
delete this._storage[size]
this._size--
return deletedData
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23