import java.util.Arrays;
public class loopQueue <E>{
public Object[] data=null;
private int maxsize;
private int rear;//队尾
private int front;//
private int size=0;
public loopQueue(){
}
public loopQueue(int initsize){
if(initsize>0){
this.maxsize=initsize;
data=new Object[initsize];
rear=front=0;
}
else{
throw new RuntimeException("初始化大小不能小于0");
}
}
//
public boolean isempty(){
return size==0;
}
//insert
public boolean add(E e){
if(size==maxsize){
throw new RuntimeException("the queue is full");
}
else{
data[rear]=e;
rear=(rear+1)%maxsize;
size++;
return true;
}
}
//返回队首元素
public E peek(){
if(isempty()){
throw new RuntimeException("this queue is empty");
}
else{
return (E)data[front];
}
}
//output
public E poll(){
if(isempty()){
throw new RuntimeException("this queue is empty");
}
else{
E e=(E)data[front];
data[front]=null;
front=(front+1)%maxsize;
size--;
return e;
}
}
//
public int getsize(){
return size;
}
//
public void clear(){
Arrays.fill(data, null);
size=0;
front=0;
rear=0;
}
}