栈特点:
1.在栈顶添加或删除
2.有序
3.元素只能通过列表的一端访问
4.后入先出(LIFO)
栈的三个主要方法 push() pop() peek();
function Stack() {
this.top = 0;
this.dataStore = [];
this.push = push;
this.pop = pop;
this.peek = peek;
this.clear = clear;
this.length = length;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top-1];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}
var stack = new Stack();
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.push("5");
stack.push("6");
console.log(stack.pop());
console.log(stack.peek());原文地址:http://51web.blog.51cto.com/4386311/1638273