栈
先进后出
用数组模仿栈实现
class Stack {
constructor() {
this.items = []
}
}
1、插入方法
push(element) {
this.items.push(element)
}
2、推出办法
pop() {
return this.items.pop()
}
3、获取栈顶元素
peek() {
return this.items[this.items.length - 1]
}
4、判断是否为空
isEmpty() {
return this.item.length === 0
}
5、清空
clear() {
this.items = []
}
6、获取长度
size() {
return this.items.length
}
发表回复