队列:先进先出
function Queue(len){
let _arr = new Array(len+1);
let front = 0;
let rear = 0;
this.enQueue = function(item){
if(this.isFull()){
console.log('队列已满!');
return false;
}
_arr[rear] = item;
rear = (rear+1)%_arr.length;
}
this.deQueue = function(){
if(this.isEmpty()){
console.log('队列为空!');
return false;
}
let item = _arr[front];
front = (front+1)%_arr.length;
return item;
}
this.isEmpty = function(){
return front === rear;
}
this.isFull = function(){
return (rear+1) % _arr.length === front;
}
}
使用(front+1)%arr.length 的方式向后移动指向开头和结尾的指针,循环队列要求空一个空间来判断
来源:CSDN
作者:李常常查德
链接:https://blog.csdn.net/qq_36330972/article/details/103712953