public class QueueDemo { private int maxSize; long[] queueArray; // 队列的头,实际是数组的尾 header; 队列的尾,实际是数组的头 footer; nElems; public QueueDemo( size){ maxSize = size; queueArray = new [maxSize]; header = 0; footer = -1; nElems = 0; } 插入队列,从队尾插入 void insert( element){ if(footer == (maxSize - 1)){ footer = -1; } queueArray[++footer] = element; nElems++ 出队列,即取出数组尾 remove(){ long temp = queueArray[header]; queueArray[header] = 0; if(++header == maxSize){ header = 0; } nElems--return temp; } 取出队列头元素 getHeader(){ queueArray[header]; } 取出队列数组 [] getArray(){ queueArray; } }
队列是先进先出,可以想象成火车进隧道
?
优先级队列:
import java.util.Arrays; /** * 优先级数组 * * @author Orlion * @create 2015-09-12 */ PriorityQDemo { [] priorityqArray; footer; public PriorityQDemo( size; priorityqArray = 插入优先级数组 int i = footer; while(i > 0 && priorityqArray[i] > element){ priorityqArray[i + 1] = priorityqArray[i]; i--; } priorityqArray[i+1] = element; footer++ 出队列 priorityqArray[header]; priorityqArray[header] = 0; } 打印队列 void getPriorityq(){ System.out.print(Arrays.toString(priorityqArray)); } }
可以理解成有一定顺序的队列,也是先进先出