class MyCircularQueue {
final int[] nums;
int len = 0, front = 0, rear = -1;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
nums = new int[k];
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if (isFull()) return false;
rear = (rear + 1) % nums.length;
nums[rear] = value;
len++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if (isEmpty()) return false;
front = (front + 1) % nums.length;
len--;
return true;
}
/** Get the front item from the queue. */
public int Front() {
return isEmpty() ? -1 : nums[front];
}
/** Get the last item from the queue. */
public int Rear() {
return isEmpty() ? -1 : nums[rear];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return len == 0;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return len == nums.length;
}
}
622. 设计循环队列
最新推荐文章于 2024-05-13 20:09:44 发布