数据结构之栈的定义及构造函数
数据结构之栈定义及构造函数
- 栈的定义
- JavaScript => 定义栈结构的构造函数 (node环境)
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)
}

更多精彩