【数据结构 JavaScript版】- web前端开发精品课程【红点工场】--javascript--栈的概念

<!DOCTYPE html>
<html>

<head>
    <title></title>
</head>

<body>
    <script>
    // 【数据结构 JavaScript版】- web前端开发精品课程【红点工场】--javascript--栈的概念
    // javascript 栈的概念 
    // push() 进栈,从栈顶出栈
    // pop() 出栈,从栈顶进栈
    // peek() 查看栈顶元素
    // siEmpty() 检查是否为空
    // clear() 移除全部元素
    // size() 获取栈的长度
    var Stack = function() {
        var items = []; //对象是有成员
        // this.items = []; items将变成公有属性
        // items = [];
        // push(1);  items = [1];
        // push(2)  items = [1,2] 1为栈底
        this.push = function(element) {
            items.push(element)
        }
        this.getItems = function() {
            return items;
        }

        // 获取items最后一项
        this.peek = function() {
            return items[items.length - 1];
        }
        //  items = [1,2,3];
        // items.pop();  ==3     items = [1,2];
        this.pop = function() {
            return items.pop();
        }

        // items 长度为0 则为true
        this.isEmpty = function() {
            return items.length == 0
        }
        // 将items置空
        this.clear = function() {
            items = [];
        }
       
        // 返回items的长度
        this.size = function() {
            return items.length;
        }
    }
    </script>
</body>

</html>

  

posted @ 2018-12-14 18:46  SunLike阿理旺旺  阅读(321)  评论(0编辑  收藏  举报