classMyCircularDeque{public:/** Initialize your data structure here. Set the size of the deque to be k. */MyCircularDeque(intk):k(k),q(k),rear(k-1){}/** Adds an item at the front of Deque. Return true if the operation is * successful. */boolinsertFront(intvalue){if(isFull())returnfalse;front=(--front+k)%k;q[front]=value;++size;returntrue;}/** Adds an item at the rear of Deque. Return true if the operation is * successful. */boolinsertLast(intvalue){if(isFull())returnfalse;rear=++rear%k;q[rear]=value;++size;returntrue;}/** Deletes an item from the front of Deque. Return true if the operation is * successful. */booldeleteFront(){if(isEmpty())returnfalse;front=++front%k;--size;returntrue;}/** Deletes an item from the rear of Deque. Return true if the operation is * successful. */booldeleteLast(){if(isEmpty())returnfalse;rear=(--rear+k)%k;--size;returntrue;}/** Get the front item from the deque. */intgetFront(){returnisEmpty()?-1:q[front];}/** Get the last item from the deque. */intgetRear(){returnisEmpty()?-1:q[rear];}/** Checks whether the circular deque is empty or not. */boolisEmpty(){returnsize==0;}/** Checks whether the circular deque is full or not. */boolisFull(){returnsize==k;}private:constintk;vector<int>q;intsize=0;intfront=0;intrear;};
classMyCircularDeque{/** Initialize your data structure here. Set the size of the deque to be k. */publicMyCircularDeque(intk){this.k=k;this.q=newint[k];this.rear=k-1;}/** Adds an item at the front of Deque. Return true if the operation is successful. */publicbooleaninsertFront(intvalue){if(isFull())returnfalse;front=(--front+k)%k;q[front]=value;++size;returntrue;}/** Adds an item at the rear of Deque. Return true if the operation is successful. */publicbooleaninsertLast(intvalue){if(isFull())returnfalse;rear=++rear%k;q[rear]=value;++size;returntrue;}/** Deletes an item from the front of Deque. Return true if the operation is successful. */publicbooleandeleteFront(){if(isEmpty())returnfalse;front=++front%k;--size;returntrue;}/** Deletes an item from the rear of Deque. Return true if the operation is successful. */publicbooleandeleteLast(){if(isEmpty())returnfalse;rear=(--rear+k)%k;--size;returntrue;}/** Get the front item from the deque. */publicintgetFront(){returnisEmpty()?-1:q[front];}/** Get the last item from the deque. */publicintgetRear(){returnisEmpty()?-1:q[rear];}/** Checks whether the circular deque is empty or not. */publicbooleanisEmpty(){returnsize==0;}/** Checks whether the circular deque is full or not. */publicbooleanisFull(){returnsize==k;}privatefinalintk;privateint[]q;privateintsize=0;privateintfront=0;privateintrear;}