队列是一个有序列表
先进先出原则
public class QueueTest{
privat Object[] data=null;
private int maxsize;//队列容量
private int front;//队列头
private int last;//队列尾
QueueTest(){
this(10);
}
public QueueTest(int initsize){
if(initsize>=0){
data=new Object[initsize];
this.maxsize=initsize;
front=last=-1;
}else{
throw new runtimeException("队列长度不小于0");
}
}
//判断队列是否为空
public boolean isEmtry(){
return front==last;
}
//向队列插入元素
public boolean add(Object obj){
if(last==maxsize-1){
throw new RuntimeException("队列已满");
}else{
data[++last]=obj;
return true;
}
}
//出队列
public Object getQueue(){
if(isEmtry){
throw new Exception("队列为空");
}
front++;
return data[front];
}
}