社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
module.exports = function Stack() {
// 初始化栈仓库
const arr = []
// 压栈
this.push = item => arr.push(item)
// 弹栈
this.pop = () => arr.pop()
// 返回栈顶元素
this.top = () => arr[arr.length - 1]
// 栈的大小
this.size = () => arr.length
// 栈是否为空
this.isEmpty = () => arr.length === 0
// 清空栈
this.clear = () => arr.splice(0, arr.length)
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!