function stack(){
this.arr = [];
/**
* [size description]元素数量
* @return {[type]} [description]
*/
stack.prototype.size = function(){
return this.arr.length
}
/**
* [isEmpty description]是否为空
* @return {Boolean} [description]
*/
stack.prototype.isEmpty = function(){
let result = this.arr.length > 0 ? false : true;
return result;
}
/**
* [push description]入栈
* @param {[type]} e [description]
* @return {[type]} [description]
*/
stack.prototype.push = function(e){
this.arr.push(e);
return this.arr;
}
/**
* [pop description]出栈
* @return {[type]} [description]
*/
stack.prototype.pop = function(){
this.arr.splice(this.arr.length - 1,1);
return this.arr;
}
/**
* [top description]获取栈顶元素
* @return {[type]} [description]
*/
stack.prototype.top = function(){
return this.arr[this.arr.length - 1];
}
}
let stackTest = new stack();
console.log('isEmpty-- ',stackTest.isEmpty());
stackTest.push(11);
stackTest.push(22);
stackTest.push(33);
stackTest.push(44);
console.log('push-- ',stackTest);
console.log('size-- ',stackTest.size());
console.log('top-- ',stackTest.top());
console.log('pop-- ',stackTest.pop());
console.log('top-- ',stackTest.top());

栈.png
网友评论