在leetcode的编写循环队列问题中编写出队函数:
class MyCircularQueue {
int[] data;
int front;
int tail;
int size;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
data = new int[k + 1];
front = 0;
tail = 0;
size = 0;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(isFull()){
return false;
}
data[tail] = value;
tail = (tail + 1) % data.length;
size ++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(isEmpty()){
return false;
}
front = (front + 1) % data.length;
size --;
return true;
}
/** Get the front item from the queue. */
public int Front() {
if(isEmpty()){
return -1;
}
return data[front];
}
/** Get the last item from the queue. */
public int Rear() {
// if(isEmpty()){
// return -1;
// }
// if(tail - 1 < 0){
// return data[data.length - 1];
// }
// else{
// return data[tail - 1];
// }
return data[(tail - 1) % data.length];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return front == tail;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return (tail + 1) % data.length == front;
}
public static void main(String[] args){
MyCircularQueue obj = new MyCircularQueue(6);
boolean param_1 = obj.enQueue(6);
int param_2 = obj.Rear();
int param_3 = obj.Rear();
boolean param_4 = obj.deQueue();
boolean param_5 = obj.enQueue(5);
int param_6 = obj.Rear();
boolean param_7 = obj.deQueue();
int param_8 = obj.Front();
boolean param_9 = obj.deQueue();
boolean param_10 = obj.deQueue();
boolean param_11 = obj.deQueue();
System.out.println(param_1);
System.out.println(param_2);
System.out.println(param_3);
System.out.println(param_4);
System.out.println(param_5);
System.out.println(param_6);
System.out.println(param_7);
System.out.println(param_8);
System.out.println(param_9);
System.out.println(param_10);
System.out.println(param_11);
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
提交时front = (front + 1) % data.length;这句话总是提示执行出错是什么情况