这个题我也不太会,写在这里也是方便复习,若有大佬有好的理解方法,还请不吝赐教
首先,只要是循环队列,就需要设置两个指针分别指向队列的头和尾,但是由于是循环队列,本来我们单向队列判定队列为满的方法就无法使用了,头指针和尾指针同时指向同一个节点,无法判断是空还是满,这里我们有两种方法,一种是再引入一个变量来记录,队列中的数是否满,第二种就是将一个节点置为空,专门用来判断;这里我就使用的是第二种方法;
public class MyCircularDeque {
private int capacity;
private int[] arr;
private int front;
private int rear;
/**
* Initialize your data structure here. Set the size of the deque to be k.
*/
public MyCircularDeque(int k) {
capacity = k + 1;
arr = new int[capacity];
//开始由于没有值进入,所以头和尾都指向同一个节点
// 头部指向第 1 个存放元素的位置
// 插入时,先减,再赋值(这里可以理解为头只能往前走,顺时针)
// 删除时,索引 +1(注意取模)
front = 0;
// 尾部指向下一个插入元素的位置
// 插入时,先赋值,再加(尾只能往后走,)
// 删除时,索引 -1(注意取模)
rear = 0;
}
/**
* Adds an item at the front of Deque. Return true if the operation is successful.
*/
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
front = (front - 1 + capacity) % capacity;
arr[front] = value;
return true;
}
/**
* Adds an item at the rear of Deque. Return true if the operation is successful.
*/
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
arr[rear] = value;
rear = (rear + 1) % capacity;
return true;
}
/**
* Deletes an item from the front of Deque. Return true if the operation is successful.
*/
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
// front 被设计在数组的开头,所以是 +1
front = (front + 1) % capacity;
return true;
}
/**
* Deletes an item from the rear of Deque. Return true if the operation is successful.
*/
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
// rear 被设计在数组的末尾,所以是 -1
rear = (rear - 1 + capacity) % capacity;
return true;
}
/**
* Get the front item from the deque.
*/
public int getFront() {
if (isEmpty()) {
return -1;
}
return arr[front];
}
/**
* Get the last item from the deque.
*/
public int getRear() {
if (isEmpty()) {
return -1;
}
// 当 rear 为 0 时防止数组越界
return arr[(rear - 1 + capacity) % capacity];
}
/**
* Checks whether the circular deque is empty or not.
*/
public boolean isEmpty() {
return front == rear;
}
/**
* Checks whether the circular deque is full or not.
*/
public boolean isFull() {
// 注意:这个设计是非常经典的做法
return (rear + 1) % capacity == front;
}
}
2
class MyCircularDeque {
int[] myqueue ;
int front;//队头指针
int rear;//队尾指针
int size;//队列当前的大小
int capacity;//队列的容量
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
this.myqueue = new int[k];
this.front = 0;
this.rear = 0;
this.size = 0;
this.capacity = k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(rear==front && size == capacity) return false;//如果队列满,插入失败
else {
front = (front + capacity -1)% capacity;
myqueue[front] = value;
size++;
return true;
}
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(rear==front && size == capacity) return false;//如果队列满,插入失败
else {
myqueue[rear] = value;
rear = (rear+1+capacity)%capacity;
size++;
return true;
}
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if( rear == front && size == 0) return false;
else {
front = (front+1) % capacity;
size--;
return true;
}
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if( rear == front && size == 0) return false;
else {
rear = (rear - 1 + capacity) % capacity;
size--;
return true;
}
}
/** Get the front item from the deque. */
public int getFront() {
if((rear == front) && size==0) return -1;
else {
int frontE = myqueue[front];
//front = (front + 1) % capacity;
//size--;
return frontE;
}
}
/** Get the last item from the deque. */
public int getRear() {
if((rear == front) && size==0) return -1;
else {
int rearE = myqueue[(rear-1+capacity)%capacity];
// rear = (rear - 1 +capacity)%capacity;
//size--;
return rearE;
}
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return (rear == front) && size==0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return rear==front && size == capacity;
}
}