javascript数据结构对于栈的封装
function LIFO(){
this.items = []
LIFO.prototype.push = function(element){
this.items.push(element)
}
LIFO.prototype.pop = function(){
return this.items.pop()
}
LIFO.prototype.peek = function(){
return this.items[this.items.length - 1]
}
LIFO.prototype.isEmpty = function(){
return this.items.length == 0
}
LIFO.prototype.size = function(){
return this.items.length
}
LIFO.prototype.toString = function(){
var resuletString = ''
for (let i = 0; i < this.items.length; i++) {
resuletString+=this.items[i]+" "
}
return resuletString
}
}
var lifo = new LIFO();
lifo.push(10)
lifo.push(20)
lifo.push(100)
console.log(lifo.toString());