原帖:http://blog.csdn.net/q1056843325/article/details/52964790
var Stack=function(){
var items=[];
this.push=function(ele){
items.push(ele);
};//栈顶加入一个元素
this.pop=function(ele){
return items.pop();
};//栈顶移除一个元素
this.peek=function(ele){
return items[items.length-1];
};//查看栈顶元素
this.isEmpty=function(){
return items.length===0;
};//判断栈是否为空
this.size=function(){
return items.length;
};//查看栈中有几个元素
this.clear=function(){
itenms=[];
};//清楚所有栈中元素
this.print=function(){
console.log(items.toString());
};//输出栈中元素
};
var stack=new Stack();
stack.push('1');
stack.push('2');
stack.print();
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.size());
stack.pop();
stack.print();
stack.clear();
stack.print();
输出内容如下:
1,2
2
false
2
1
1









网友评论