js实现栈结构

来源:互联网 发布:淘宝怎样上传宝贝视频 编辑:程序博客网 时间:2024/06/05 16:06
var Stack = function() {    this.dataStore = [];//保存栈内元素    this.top = 0;};//栈操作,从栈原型出发Stack.prototype={    push:function push(element) {        this.dataStore[this.top++] = element;//添加一个元素并将top+1    },    peek:function peek() {        return this.dataStore[this.top-1];//返回栈顶元素    },    pop:function pop() {        return this.dataStore[--this.top];//返回栈顶元素并将top-1    },    clear:function clear() {        this.top = 0;//将top归0    },    length:function length() {        return this.top;//返回栈内的元素个数    }};
0 0
原创粉丝点击