什么是队列?

(图是转载的,代码是我本人实现的)
第二个图表示增加元素(移动rear),第三个图表示取出元素(移动front)
那么什么是循环队列呢?其实也很简单,看上面的图,你有没有发现一些问题呢?
当元素全部取出来的时候这个时候,会发现front与rear相遇了,如果继续添加元素,就会浪费掉原来储存数据的空间,而循环队列解决了这个问题,思路不多赘述了,其实就是用数组实现的,原理也很简单,实现起来也不难。直接上代码把
class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[this.maxSize];
this.front = -1; //指向队列头部的前一个位置,但是不包含
this.rear = -1; // 指向队列的尾部具体的数据
}
// 判断队列是否已满
public boolean isFull() {
return (this.maxSize - 1) == rear;
}
// 判断队列是否为空
public boolean isNull() {
return this.rear == this.front;
}
// 添加数据
public void addQueue(int n) {
if (isFull()) {
Scanner scanner = new Scanner(System.in);
System.out.println("该队列已满,如果继续添加元素则之前的数据重置,请输入‘是’或者‘否’");
char a = scanner.nextLine().charAt(0);
if (a == '是') {
this.rear = -1;
}
else {
return;
}
}
rear += 1;
this.arr[rear] = n;
}
//取出队列的数据
public int getQueue() {
//判断队列是否空
front++;
int x = this.arr[front];
if (front == rear) {
System.out.println("该队列已空");
front = -1;
rear = -1;
}
return x;
}
//显示队列的所有数据
public void show() {
int pose = this.front;
if (isNull()) {
throw new RuntimeException("队列是空的");
}
while (pose < this.rear) {
pose++;
System.out.printf("arr[%d] = %d ", pose, arr[pose]);
}
}
//显示队列的头数据
public int headQueue() {
if (isNull()) {
throw new RuntimeException("队列是null");
}
else {
return this.arr[++this.front];
}
}
}
这些代码是本人所写的,我也是尽量测试了一下,如果路过的大神发现了bug还请多多指教了