直接利用 c ++ 里的 STL 库的 双端队列 deque
添加元素:
队头: push_front()
队尾: push_back()
删除元素:
队头: pop_front()
队尾: pop_back()
取得元素:
队头: front()
队尾: back()
获得大小:
size()
判断是否空:
empty()
C++ 代码
class MyCircularDeque {
public:
int mx = 0;
deque<int> q;
MyCircularDeque(int k) {
this->mx = k;
}
bool insertFront(int value) {
if (q.size() < this->mx) {
q.push_front(value);
return true;
}
return false;
}
bool insertLast(int value) {
if (q.size() < this->mx) {
q.push_back(value);
return true;
}
return false;
}
bool deleteFront() {
if (q.empty()) return false;
q.pop_front();
return true;
}
bool deleteLast() {
if (q.empty()) return false;
q.pop_back();
return true;
}
int getFront() {
if (q.empty()) return -1;
return q.front();
}
int getRear() {
if (q.empty()) return -1;
return q.back();
}
bool isEmpty() {
return q.empty();
}
bool isFull() {
return q.size() == this->mx;
}
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque* obj = new MyCircularDeque(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/