Public classQueuedemo {Private intmaxSize; Private Long[] queuearray; //The head of the queue, which is actually the end of the array Private intheader; //The tail of the queue, which is actually the head of the array Private intfooter; Private intNelems; PublicQueuedemo (intsize) {MaxSize=size; Queuearray=New Long[MaxSize]; Header= 0; Footer=-1; Nelems= 0; } //Insert queue, insert from end of team Public voidInsertLongElement) { if(Footer = = (maxSize-1) ) {footer=-1; } queuearray[++footer] =element; Nelems++; } //out of the queue, that is, the end of the array Public LongRemove () {Longtemp =Queuearray[header]; Queuearray[header]= 0; if(++header = =maxSize) {Header= 0; } nelems--; returntemp; } //Remove Queue Header element Public LongGetHeader () {returnQueuearray[header]; } //Remove Queue Array Public Long[] GetArray () {returnQueuearray; }}
The queue is first-out, can be imagined as a train into the tunnel
Priority queue:
Importjava.util.Arrays;/*** Priority Series Group * *@authororlion * @create 2015-09-12*/ Public classPriorityqdemo {Private intmaxSize; Private Long[] priorityqarray; Private intheader; Private intfooter; PublicPriorityqdemo (intsize) {MaxSize=size; Priorityqarray=New Long[MaxSize]; Header= 0; Footer=-1; } //Insert Priority Series Group Public voidInsertLongElement) { inti =footer; while(i > 0 && priorityqarray[i] >Element) {Priorityqarray[i+ 1] =Priorityqarray[i]; I--; } priorityqarray[i+1] =element; Footer++; } //out Queue Public LongRemove () {Longtemp =Priorityqarray[header]; Priorityqarray[header]= 0; if(++header = =maxSize) {Header= 0; } returntemp; } //print queue Public voidGetpriorityq () {System.out.print (arrays.tostring (Priorityqarray)); }}
A queue that can be understood as a certain order, and also a FIFO
Queue-java Code