public class ArrayQueue {
//队列容量
public int size;
//队列头部下标
public int front;
//队列尾部下标
public int rear;
//数组
public int arr[];
public ArrayQueue(int size){
this.size = size + 1; //默认留一个空地址
this.front = 0;
this.rear = 0;
this.arr = new int[size+1];
}
public boolean isFull(){
return (rear+1) % size == front;
}
public boolean isEmpty(){
return rear == front;
}
public void push(int item){
if(isFull()){
throw new RuntimeException("队列已满,入队失败");
}
arr[rear] = item;
rear = (rear + 1) % size;
}
public int pop(){
if(isEmpty()){
throw new RuntimeException("队列为空");
}
int item = arr[front];
front = (front + 1) % size;
return item;
}
public int length(){
return (rear + size - front) % size;
}
public void show(){
if(isEmpty()){
throw new RuntimeException("队列为空");
}
for (int i = front; i < front+length(); i++) {
System.out.print(arr[i%size]+"t");
}
System.out.printf("n");
}
public int getHeader(){
if(isEmpty()){
throw new RuntimeException("队列为空");
}
return arr[front];
}
}