One, priority queue
What is a priority queue: A priority queue is a data structure that is more commonly used than stacks and queues. In the priority queue, the data items are ordered by the value of the keyword. When data items are inserted into a queue, they are inserted sequentially into the appropriate position to guarantee the order of the queues.
Examples of life, suppose you have a number of pieces, the files you most need to process are placed at the top of all messages, and if the files you don't need to deal with are placed below.
Reference code:
PackageEdu.structure.queue; Public classPriorityq {Private intmaxSize; Private int[] quearray; Private intNelmes; PublicPRIORITYQ (intmaxSize) { This. maxSize =maxSize; Quearray=New int[MaxSize]; Nelmes= 0; } Public voidInsertintvalue) { if(Nelmes = = 0) {Quearray[nelmes++] =value; } Else { inti; for(i = nElmes-1; I >= 0; i--) { if(Value >Quearray[i]) {Quearray[i+ 1] =Quearray[i]; } Else { Break; }} quearray[i+ 1] =value; Nelmes++; } } Public intRemove () {returnquearray[--Nelmes]; } Public BooleanIsfull () {return(Nelmes = =maxSize); } Public BooleanIsEmpty () {return(Nelmes = = 0); }}
Second, the test code
Public Static void Main (string[] args) { priorityq priorityq=new priorityq (ten); Priorityq.insert (ten); Priorityq.insert (5); Priorityq.insert (a); Priorityq.insert (a); while (! Priorityq.isempty ()) { System.out.println (Priorityq.remove ()); } }
Java data structure and algorithm value priority queue