使用数组的数据结构,采用Java代码实现循环双端队列
时间: 2023-05-28 15:04:30 浏览: 131
```
public class MyCircularDeque {
private int[] deque;
private int front;
private int rear;
private int size;
private int capacity;
public MyCircularDeque(int k) {
deque = new int[k];
front = 0;
rear = 0;
size = 0;
capacity = k;
}
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
front = (front - 1 + capacity) % capacity;
deque[front] = value;
size++;
return true;
}
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
deque[rear] = value;
rear = (rear + 1) % capacity;
size++;
return true;
}
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
front = (front + 1) % capacity;
size--;
return true;
}
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
rear = (rear - 1 + capacity) % capacity;
size--;
return true;
}
public int getFront() {
if (isEmpty()) {
return -1;
}
return deque[front];
}
public int getRear() {
if (isEmpty()) {
return -1;
}
return deque[(rear - 1 + capacity) % capacity];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
}
```
阅读全文