一、题目
二、代码
从这道题的测试用例可以看出这种循环队列font和rear初始化时,font位于-1,rear位于0,和传统的循环队列有点区别。因此刷力扣前,一定要看准测试用例,以免浪费时间。
class MyCircularQueue {
private int[] queue;
private int font;
private int rear = -1;
private int size;
private int eleNum;
public MyCircularQueue(int k) {
queue = new int[k];
size = k;
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
rear = (rear + 1) % size;
queue[rear] = value;
eleNum++;
return true;
}
public boolean deQueue() {
if (isEmpty()) {
return false;
}
font = (font + 1) % size;
eleNum--;
return true;
}
public int Front() {
if (isEmpty()) {
return -1;
}
return queue[font];
}
public int Rear() {
if (isEmpty()) {
return -1;
}
return queue[rear];
}
public boolean isEmpty() {
return eleNum == 0;
}
public boolean isFull() {
return eleNum == size;
}
public static void main(String[] args) {
MyCircularQueue obj = new MyCircularQueue(3);
boolean param_1 = obj.enQueue(1);
boolean param_2 = obj.enQueue(2);
boolean param_3 = obj.enQueue(3);
boolean param_4 = obj.enQueue(4);
int rear = obj.Rear();
boolean isFull = obj.isFull();
boolean param_5 = obj.deQueue();
boolean param_6 = obj.enQueue(4);
int rear_ = obj.Rear();
System.out.println(param_1 + " " + param_2 + " " + param_3 + " " + param_4 + " " + rear + " " + isFull + " "
+ param_5 + " " + param_6 + " " + rear_);
}
}