利用数组创建队列结构
public class ArrQueue {
private int[] arr;
private int pushi;
private int polli;
private int size;
private final int limit;
public ArrQueue(int limit) {
arr = new int[limit];
pushi = 0;
polli = 0;
size = 0;
this.limit = limit;
}
//向队列添加数
public void push(int value){
if(size == limit){
throw new RuntimeException("队列满了");
}
size++;
arr[pushi] = value;
pushi = nextIndex(pushi);
}
//向队列拿数据
public int pop(){
if(size == 0){
throw new RuntimeException("队列中无数据");
}
int res = arr[polli];
size--;
polli = nextIndex(polli);
return res;
}
public boolean isEmpty(){
return size==0;
}
private int nextIndex(int i){
return i<limit-1? i++: 0;
}
}