我试图实现一个没有计数变量的循环数组队列。
我正在寻找一种方法来确定数组是否已满,以便调用 expand 方法,但它似乎不知道为什么?谢谢!
public void enqueue (T element) {
if(front != (rear+1) % queue.length) {
queue[rear] = element;
rear = (rear+1) % queue.length;
}
else
expandCapacity();
}
public void expandCapacity() {
T[] larger = ((T[]) (new Object[queue.length * 2]));
for (int scan=0; scan < queue.length; scan++) {
larger[scan] = queue[front];
front = (front+1) % queue.length;
}
front = 0;
rear = queue.length;
queue = larger;
}