javascript数据结构探险之栈的实现

        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;
            };//检测栈内元素是否为零,如果空栈返回true,否则返回false;
            this.size=function(){
                return items.length;
            };//检测栈的长度
            this.clear=function(){
                items=[];
            };//清空栈
            this.print=function(){
                console.log(items.toString());
            };//输出栈内元素
        }

 

posted @ 2017-02-28 12:44  小金鱼有点笨  阅读(118)  评论(0)    收藏  举报