classMyCircularQueue{public:/** Initialize your data structure here. Set the size of the queue to be k. */MyCircularQueue(intk):k(k),q(k),rear(k-1){}/** Insert an element into the circular queue. Return true if the operation is * successful. */boolenQueue(intvalue){if(isFull())returnfalse;rear=++rear%k;q[rear]=value;++size;returntrue;}/** Delete an element from the circular queue. Return true if the operation is * successful. */booldeQueue(){if(isEmpty())returnfalse;front=++front%k;--size;returntrue;}/** Get the front item from the queue. */intFront(){returnisEmpty()?-1:q[front];}/** Get the last item from the queue. */intRear(){returnisEmpty()?-1:q[rear];}/** Checks whether the circular queue is empty or not. */boolisEmpty(){returnsize==0;}/** Checks whether the circular queue is full or not. */boolisFull(){returnsize==k;}private:constintk;vector<int>q;intsize=0;intfront=0;intrear;};
classMyCircularQueue{/** Initialize your data structure here. Set the size of the queue to be k. */publicMyCircularQueue(intk){this.k=k;this.q=newint[k];this.rear=k-1;}/** Insert an element into the circular queue. Return true if the operation is successful. */publicbooleanenQueue(intvalue){if(isFull())returnfalse;rear=++rear%k;q[rear]=value;++size;returntrue;}/** Delete an element from the circular queue. Return true if the operation is successful. */publicbooleandeQueue(){if(isEmpty())returnfalse;front=++front%k;--size;returntrue;}/** Get the front item from the queue. */publicintFront(){returnisEmpty()?-1:q[front];}/** Get the last item from the queue. */publicintRear(){returnisEmpty()?-1:q[rear];}/** Checks whether the circular queue is empty or not. */publicbooleanisEmpty(){returnsize==0;}/** Checks whether the circular queue is full or not. */publicbooleanisFull(){returnsize==k;}privatefinalintk;privateint[]q;privateintsize=0;privateintfront=0;privateintrear;}