队列的特点先进先出
Queue.java(队列类)
package sortedListApp;
/**
* 队列
* @author qingshuang
*
*/
public class Queue {
private long[] queArray;
private int maxSize;//数组的大小
private int front;//头指针
private int rear;//尾部指针
private int nItems;//存了多少元素
public Queue(int s){
maxSize=s;
queArray=new long[maxSize];
front=0;
rear=-1;
}
//插入数据
public void insert(long j){
if(rear==maxSize-1)rear=-1;
queArray[++rear]=j;
nItems++;
}
//移除数据
public long remove(){
long temp=queArray[front++];
if(front==maxSize)front=0;
nItems--;
return temp;
}
//查看数据
public long peeFront(){
return queArray[front];
}
//查看是不是为空
public boolean isEmpty(){
return nItems==0;
}
//查看是不是满的
public boolean isFull(){
return nItems==maxSize;
}
//查看元素个数
public int size(){
return nItems;
}
}
QueueApp.java(测试类)
package sortedListApp;
/***
* 队列测试类
* @author qingshuang
*
*/
public class QueueApp {
public static void main(String[] args) {
Queue theQueue=new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
System.out.println("删除: "+theQueue.remove());
System.out.println("删除: "+theQueue.remove());
System.out.println("删除: "+theQueue.remove());
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while(!theQueue.isEmpty()){
long n=theQueue.remove();
System.out.print(n+" ");
}
}
}