641. 设计循环双端队列
class MyCircularDeque {
public:
vector<int> arr;
int cnt, head, tail;
MyCircularDeque(int k): arr(k),head(0), tail(0), cnt(0) {}
bool insertFront(int value) {
if(isFull()) return false;
head = head - 1;
if(head == -1) head = arr.size() - 1;
arr[head] = value;
cnt += 1;
return true;
}
bool insertLast(int value) {
if(isFull()) return false;
arr[tail] = value;
tail += 1;
if(tail == arr.size()) tail = 0;
cnt += 1;
return true;
}
bool deleteFront() {
if(isEmpty()) return false;
head = (head + 1) % arr.size();
cnt -= 1;
return true;
}
bool deleteLast() {
is(isEmpty()) return false;
tail = (tail - 1 + arr.size()) % arr.size();
cnt -= 1;
return true;
}
int getFront() {
if(isEmpty()) return -1;
return arr[head];
}
int getRear() {
if(isEmpty()) return -1;
return arr[(tail - 1 + arr.size()) % arr.size()];
}
bool isEmpty() {
return cnt == 0;
}
bool isFull() {
return cnt == arr.size();
}
};