美文网首页
JS中定义一个栈结构

JS中定义一个栈结构

作者: LO_0668 | 来源:发表于2019-04-21 19:41 被阅读0次
    // 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
                    }
                }
            }
    

    相关文章

      网友评论

          本文标题:JS中定义一个栈结构

          本文链接:https://www.haomeiwen.com/subject/xznjgqtx.html