Java数据结构--基于数组的队列
public class Queue {
private int[] data;
private int head;
private int back;
private int size;
public Queue(){
data = new int[20];
}
public void append(int x){
if(this.size == data.length -1){
copy();
}
data[back] = x;
back = (back+1)%data.length;
size += 1;
}
public int peek(){
size -= 1;
int y = data[head];
head = (head+1)%data.length;
return y;
}
private void copy(){
int[] data_new = new int[size*2];
for(int i=0;i<data.length;i++){
data_new[i] = data[i];
}
this.data = data_new;
}
}