作者: skoll | 来源:发表于2020-05-24 15:03 被阅读0次

    简介

    1 .栈内的元素只能通过列表的一端访问,栈顶
    2 .先入后出
    3 .任何不在栈顶的元素都无法访问

    class Stack{
        constructor(){
            this.dataStore=[]
            this.top=0
        }
        push(el){
            return this.dataStore[this.top++]=el
        }
        top(){
            return this.dataStore[--this.top]
        }
        clear(){
            this.dataStore=[]
            return this.top=0
        }
        length(){
            return this.top
        }
    }
    
    var s = new Stack();
    s.push("David");
    s.push("Raymond");
    s.push("Bryan");
    console.log(s.length())
    
    s.clear();
    console.log(s.length());
    
    

    相关文章

      网友评论

          本文标题:

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