https://leetcode.com/problems/design-circular-deque/
- single array
- two pointers (front & end), wrap them around accordingly
class MyCircularDeque {
int *arr, front, end, k;
public:
MyCircularDeque(int k) {
arr = new int[k + 1];
front = 0;
end = k;
this->k = k;
}
bool insertFront(int value) {
if (isFull()) return false;
arr[front] = value;
++front;
if (front == k + 1) front = 0;
return true;
}
bool insertLast(int value) {
if (isFull()) return false;
arr[end] = value;
--end;
if (end == -1) end = k;
return true;
}
bool deleteFront() {
if (isEmpty()) return false;
--front;
if (front == -1) front = k;
return true;
}
bool deleteLast() {
if (isEmpty()) return false;
++end;
if (end == k + 1) end = 0;
return true;
}
int getFront() {
if (isEmpty()) return -1;
return (front - 1 < 0)? arr[k] : arr[front - 1];
}
int getRear() {
if (isEmpty()) return -1;
return (end + 1 > k)? arr[0] : arr[end + 1];
}
bool isEmpty() {
return (front - 1 < 0)? (end == k) : (front - 1 == end);
}
bool isFull() {
return front == end;
}
};