码迷,mamicode.com
首页 > 编程语言 > 详细

javascript栈的建立样码

时间:2016-08-28 12:20:04      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

早上参加小孩的一年级入学前,看看相关的东东啦。。

技术分享

function Stack() {
    var items = [];

    this.push = function(element){
        items.push(element);
    }

    this.pop = function(){
        return items.pop();
    }

    this.peek = function() {
        return items[items.length -1];
    }

    this.isEmpty = function() {
        return items.length == 0;
    }

    this.size = function() {
        return items.length;
    }

    this.clear = function(){
        items = [];
    }

    this.print = function() {
        console.log(items.toString());
    }
}

var stack = new Stack();
console.log(stack.isEmpty());

stack.push(5);
stack.push(8);
console.log(stack.peek());
stack.push(11);
console.log(stack.size());
console.log(stack.isEmpty());
stack.push(15);
stack.pop();
stack.pop();
console.log(stack.size());
stack.print();

技术分享

javascript栈的建立样码

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5814675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!