队列是先进先出。
利用java语言实现队列代码:
/*
* 队列
*/
public class Queue {
private int maxSize; //最大队列
private long[] queArray; //队列数组
private int front; //队头
private int rear; //队尾
private int nItems; //当前队列元素个数
//构造函数
public Queue(int s) {
super();
this.maxSize = s;
this.queArray = new long[maxSize];
this.front = 0;
this.rear = -1;
this.nItems = 0;
}
//插入元素
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 peekFront() {
return queArray[front];
}
//队列是否为空
public boolean isEmpty(){
return (nItems ==0);
}
//队列是否满了
public boolean isFull() {
return(nItems==maxSize);
}
//队列长度
public int size() {
return nItems;
}
}
<pre name="code" class="java">public class QueueApp {
public static void main (String[] arg) {
Queue theQueue =new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
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);
System.out.print(" ");
}
System.out.print(" ");
}
}
原文地址:http://blog.csdn.net/cool_easy/article/details/45703815