-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathstack-array.js
More file actions
56 lines (45 loc) · 835 Bytes
/
Copy pathstack-array.js
File metadata and controls
56 lines (45 loc) · 835 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
54
55
56
class Stack {
constructor() {
this.items = []
}
/** 入栈 */
push(element) {
this.items.push(element)
}
/** 出栈 */
pop() {
return this.items.pop()
}
/** 栈顶元素 */
peek() {
if (this.isEmpty()) return undefined
return this.items[this.items.length - 1]
}
/** 栈是否为空 */
isEmpty() {
return this.items.length === 0
}
/** 清空栈 */
clear() {
this.items = []
}
/** 栈长 */
size() {
return this.items.length
}
/** 拓展-查看所有栈 */
toArray() {
return this.items
}
/** 拓展-输出当前栈 */
toString() {
return this.items.toString()
}
}
// test
const xxx = new Stack()
xxx.push(3)
xxx.push(2)
console.log('pop = ', xxx.pop());
console.log('peek = ', xxx.peek());
console.log('toString = ', xxx.toString());