622. Design Circular Queue
设计一个环状伫列。
Explanation
MyCircularQueue myCircularQueue = new MyCircularQueue(3);
myCircularQueue.enQueue(1); // return True
myCircularQueue.enQueue(2); // return True
myCircularQueue.enQueue(3); // return True
myCircularQueue.enQueue(4); // return False
myCircularQueue.Rear(); // return 3
myCircularQueue.isFull(); // return True
myCircularQueue.deQueue(); // return True
myCircularQueue.enQueue(4); // return True
myCircularQueue.Rear(); // return 4
思路:
1.照定义去实现
2.用module size来实现环状的效果
Java Code:
class MyCircularQueue {
private int max_size, first, last, size;
private int[] queue;
public MyCircularQueue(int k) {
max_size = k;
size = 0;
first = 0;
last = -1;
queue = new int[max_size];
}
public boolean enQueue(int value) {
if(isFull()) return false;
size++;
last = (last + 1) % max_size;
queue[last] = value;
return true;
}
public boolean deQueue() {
if(isEmpty()) return false;
size