// js中栈的定义
function Stack() {
this.top = 0
this.arr = []
this.push = push
this.pop = pop
this.peek = peek
this.clear = clear
this.len = len
this.isEmpty = isEmpty
function push(ele) {
this.arr[this.top++] = ele
}
function pop() {
console.log('top:',this.top)
return this.arr[--this.top]
}
function peek() {
return this.arr[this.top-1]
}
function clear() {
delete this.arr
this.top = 0
this.arr = []
}
function len() {
return this.top
}
function isEmpty() {
if (this.len() <= 0) {
return true
} else {
return false
}
}
}
网友评论