641.設計迴圈雙端佇列
阿新 • • 發佈:2018-12-29
class MyCircularDeque {
public:
vector<int>data;
int size;
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
size = k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront (int value) {
if (isFull())
return false;
if (isEmpty())
data.push_back(value);
else
data.insert(data.begin(), value);
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull())
return false;
data.push_back(value);
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty())
return false;
data.erase(data.begin());
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty())
return false;
data.pop_back();
return true;
}
/** Get the front item from the deque. */
int getFront() {
if (isEmpty())
return -1;
return *data.begin();
}
/** Get the last item from the deque. */
int getRear() {
if (isEmpty())
return -1;
return data.back();
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
if (data.empty())
return 1;
return 0;
}
/** Checks whether the circular deque is full or not. */
bool isFull() {
if (data.size() == size)
return 1;
return 0;
}
};