-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.js
More file actions
53 lines (53 loc) · 1021 Bytes
/
test.js
File metadata and controls
53 lines (53 loc) · 1021 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
53
/*
* @Author: 张宏亮
* @Date: 2019-09-22 13:05:14
* @LastEditors: 张宏亮<hongliang@yunshan.net>
* @LastEditTime: 2019-09-22 14:38:26
* @Description: file content
* @Versions: 1.0.0
*/
const items = new WeakMap()
export default class Stack {
constructor() {
items.set(this, [])
}
push(element) {
const s = items.get(this)
s.push(element)
}
pop() {
const s = items.get(this)
const r = s.pop()
return r
}
peek() {
if (this.isEmpty()) {
return undefined
}
return this._items[this.count - 1]
}
isEmpty() {
return this.count === 0
}
size() {
const s = items.get(this)
return s.length
}
clear() {
/* while (!this.isEmpty()) {
this.pop();
} */
this._items = {}
this.count = 0
}
toString() {
if (this.isEmpty()) {
return ''
}
let objString = `${this._items[0]}`
for (let i = 1; i < this.count; i++) {
objString = `${objString},${this._items[i]}`
}
return objString
}
}