public class MyCircularQueue {
private int front;
private int rear;
private int capacity;
private int[] arr;
/**
* Initialize your data structure here. Set the size of the queue to be k.
*/
public MyCircularQueue(int k) {
capacity = k + 1;
arr = new int[capacity];
// 在 front 出队,故设计在数组的头部,方便删除元素
// 删除元素的时候,只索引 +1(注意取模)
// 在 rear 入队,故设计在数组的尾部,方便插入元素
// 插入元素的时候,先赋值,后索引 +1(注意取模)
front = 0;
rear = 0;
}
/**
* Insert an element into the circular queue. Return true if the operation is successful.
*/
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
arr[rear] = value;
rear = (rear + 1) % capacity;
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) % capacity;
return true;
}
/**
* Get the front item from the queue.
*/
public int Front() {
if (isEmpty()) {
return -1;
}
return arr[front];
}
/**
* Get the last item from the queue.
*/
public int Rear() {
if (isEmpty()) {
return -1;
}
return arr[(rear - 1 + capacity) % capacity];
}
/**
* Checks whether the circular queue is empty or not.
*/
public boolean isEmpty() {
return front == rear;
}
/**
* Checks whether the circular queue is full or not.
*/
public boolean isFull() {
// 注意:这是这个经典设计的原因
return (rear + 1) % capacity == front;
}
}
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/design-circular-queue/solution/shu-zu-shi-xian-de-xun-huan-dui-lie-by-liweiwei141/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。