Queue: First-out (FIFO).
Priority queue: In the priority queue, the data items are ordered by the value of the keyword, the data items with the smallest key are always the same, and the data items are inserted into the appropriate position in order to ensure the order of the queues, and the data items that are smaller than the inserted items are moved from backward to forward. The priority queue is applied in the graph's minimum spanning tree algorithm.
Example code:
Packagechap04. Queue;classQueue {Private intmaxSize; Private Long[] quearray; Private intFront; Private intRear; Private intNitems; PublicQueue (ints) {maxSize=s; Quearray=New Long[MaxSize]; Front= 0; Rear=-1; Nitems= 0; } //Insert method, the end of the queue is the last item in the array Public voidInsertLongj) {if(Rear = = MaxSize-1) {Rear=-1; } quearray[++rear] =J; Nitems++; } //Advanced First Out Public LongRemove () {Longtemp = quearray[front++]; if(Front = =maxSize) {Front= 0; } nitems--; returntemp; } Public LongPeekfront () {returnQuearray[front]; } Public BooleanIsEmpty () {return(Nitems = = 0); } Public BooleanIsfull () {return(Nitems = =maxSize); } Public intsize () {returnNitems; }}classPriorityq {Private intmaxSize; Private Long[] quearray; Private intNitems; PublicPRIORITYQ (ints) {maxSize=s; Quearray=New Long[MaxSize]; Nitems= 0; } //inserting methods, ranging from large to small Public voidInsertLongItem) { intJ; if(Nitems = = 0) {Quearray[nitems++] =item; } Else { for(j = nItems-1; J >= 0; j--) { if(Item > QUEARRAY[J]) {//if new item larger,Quearray[j + 1] =Quearray[j]; } Else { Break; }} quearray[j+ 1] =item; Nitems++; } } //to be removed from the back to the priority level, no longer related to advanced or backward Public LongRemove () {returnquearray[--Nitems]; } Public Longpeekmin () {returnQuearray[nitems-1]; } Public BooleanIsEmpty () {return(Nitems = = 0); } Public BooleanIsfull () {return(Nitems = =maxSize); }}classQueueapp { Public Static voidMain (string[] args) {Queue thequeue=NewQueue (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 ()) { Longn =Thequeue.remove (); System.out.print (n); // Max, Max, Max.System.out.print (""); } System.out.println (""); Priorityq THEPQ=NewPRIORITYQ (5); Thepq.insert (30); Thepq.insert (50); Thepq.insert (10); Thepq.insert (40); Thepq.insert (20); while(!Thepq.isempty ()) { Longitem =Thepq.remove (); System.out.print (Item+ " ");//The ten , the.} System.out.println (""); }}
Java Data Structures and algorithms (4)-Queues (queue and PRIORITYQ)