let Queue = function(){
this.items = [];
/**
* [add description]添加
* @param {[type]} el [description]
*/
Queue.prototype.add = function(el){
return this.items.push(el);
}
/**
* [element description]获取队列头元素
* @return {[type]} [description]
*/
Queue.prototype.element = function() {
return this.items[0];
}
/**
* [poll description]移出并返回队列的头元素
* @return {[type]} [description]
*/
Queue.prototype.poll = function() {
return this.items.shift();
}
/**
* [isEmpty description]队列中是否有元素
* @return {[type]} [description]
*/
Queue.prototype.isEmpty = function() {
let result = this.items.length > 0 ? false : true;
return result;
}
/**
* [size description]队列长度
* @return {[type]} [description]
*/
Queue.prototype.size = function() {
return this.items.length;
}
/**
* [clear description]清空队列
* @return {[type]} [description]
*/
Queue.prototype.clear = function(){
return this.items = [];
}
}
let queue = new Queue();
console.log('isEmpty-- ',queue.isEmpty())
queue.add('111');
queue.add('222');
queue.add('333');
queue.add('444');
console.log(queue)
console.log('element-- ',queue.element())
console.log('poll-- ',queue.poll())
console.log('size-- ',queue.size())
console.log('clear-- ',queue.clear())
队列.png
网友评论