- class MyCircularQueue {
- int[] que;
- int size=0;
- int head=0;
- int tail=0;
- /** Initialize your data structure here. Set the size of the queue to be k. */
- public MyCircularQueue(int k) {
- que=new int[k+1];
- size=k+1;
- }
- /** Insert an element into the circular queue. Return true if the operation is successful. */
- public boolean enQueue(int value) {
- if(isFull())return false;
- que[tail]=value;
- tail=(tail+1)%size;
- return true;
- }
- /** Delete an element from the circular queue. Return true if the operation is successful. */
- public boolean deQueue() {
- if(isEmpty())return false;
- head=(head+1)%size;
- return true;
- }
- /** Get the front item from the queue. */
- public int Front() {
- if(isEmpty())return -1;
- return que[head];
- }
- /** Get the last item from the queue. */
- public int Rear() {
- if(isEmpty())return -1;
- int temp=(tail-1+size)%size;
- return que[temp];
- }
- /** Checks whether the circular queue is empty or not. */
- public boolean isEmpty() {
- if(tail==head)return true;
- return false;
- }
- /** Checks whether the circular queue is full or not. */
- public boolean isFull() {
- if((tail+1)%size==head)return true;
- return false;
- }
- }
来源: http://www.bubuko.com/infodetail-2851367.html