js算法练习--栈

      class stack{
            constructor (){
                this.list=[];
            }
            //入栈
            Push(item){
                this.list.push(item);
            }
            //出站
            Pop(){
                return this.list.pop();
            }
            //判断栈是否为空
            GetIsEmpty(){
                return this.list.length==0;
            }
            //栈的大小
            GetSize(){
                return this.list.length;
            }
            //清空栈
            Clear(){
                this.list=[];
            }
            //读出栈数据
            Read(){
                console.log(this.list.toString());
            }
        }

        //使用
        let stackInfo=new stack();
        stackInfo.GetIsEmpty();//true
        stackInfo.Push(11);
        stackInfo.Push(10);
        stackInfo.Read();//11,10
        stackInfo.GetSize();//2
        stackInfo.Pop();//10
        stackInfo.Read();//11

猜你喜欢

转载自www.cnblogs.com/shuajing/p/11319035.html