/**
* 循环队列 -- 数组实现
*/
public class CircularQueue {
private String[] items;
private int n; // 队列大小
private int head = 0;
private int tail = 0;
public CircularQueue(int n) {
this.items = new String[n];
this.n = n;
}
// 入队
public boolean enqueue(String item){
if((tail+1)%n == head)
return false;
items[tail] = item;
tail = (tail+1) % n;
return true;
}
// 出队
public String dequeue(){
if(head == tail) return null;
String str = items[head];
head = (head+1) % n;
return str;
}
// 打印队列信息
public void print(){
if(head == tail) return;
int i = head;
while(i != tail){
System.out.print(items[i]+" ");
i = (i+1)%n;
}
System.out.println();
}
}