<script>
function Stack(){
this.items=[]
// this.push=function (){} 这种是在每个实例对象上直接建立一个实例对象 而下面那个是在原型上公用一个
//将一个元素压入栈
Stack.prototype.push=function (e){
this.items.push(e)
}
//从栈中取出元素
Stack.prototype.pop=function (){
return this.items.pop()
}
//查看栈顶元素
Stack.prototype.peek=function (){
return this.items[this.items.length-1]}
//判断栈是否为空
Stack.prototype.isEmpty=function (){
return this.items.length==0
}
//获取栈中的个数
Stack.prototype.size=function (){
return Stack.items.length
}
//toString方法
Stack.prototype.toString=function (){
var reslutstring=' '
for(var i=0;i<this.items.length;i++){
reslutstring=this.items[i]+' '
}
return reslutstring
}
}
</script>
数据结构:栈的初始化(JavaScript)
最新推荐文章于 2022-11-21 01:43:26 发布