题目

思路

- 所以,head可能出现在任何位置。
- 获取队尾的inedx:tailIndex = (headIndex + count − 1) mod capacity
代码
class MyCircularQueue {
private int[] queue;
private int headIndex;
private int count;
private int capacity;
public MyCircularQueue(int k) {
this.capacity = k;
this.queue = new int[k];
this.headIndex = 0;
this.count = 0;
}
public boolean enQueue(int value) {
if (this.count == this.capacity)
return false;
this.queue[(this.headIndex + this.count) % this.capacity] = value;
this.count += 1;
return true;
}
public boolean deQueue() {
if (this.count == 0)
return false;
this.headIndex = (this.headIndex + 1) % this.capacity;
this.count -= 1;
return true;
}
public int Front() {
if (this.count == 0)
return -1;
return this.queue[this.headIndex];
}
public int Rear() {
if (this.count == 0)
return -1;
int tailIndex = (this.headIndex + this.count - 1) % this.capacity;
return this.queue[tailIndex];
}
public boolean isEmpty() {
return (this.count == 0);
}
public boolean isFull() {
return (this.count == this.capacity);
}
}