1 /*Priority Queue--an improvement to the queue2 * Priority is given to the data being stored--small priority--in the team header3 * Implementation of priority queue4 array: Suitable for small data volumes (not implemented with Rear+front)5 * Priority Queue header in Items-1, queue tail at 0 is fixed6 heap: Suitable for large volumes of data7 * Efficiency of priority queue: Insert O (N) Remove O (1)8 * Application of Priority queue: Operating system thread scheduling algorithm9 * */Ten Public classMypriorityqueue { One Private intmaxSize; A Private Long[] arr;//guaranteed to be orderly when inserted - Private intitems; - the PublicMypriorityqueue (ints) { -MaxSize =s; -arr =New Long[maxSize]; -Items = 0; + } - + Public voidInsertLongkey) { A intJ; at if(items = = 0) {//join directly to empty -arr[items++] =key; - } - Else{//do not empty the small element on the top--Queue header - for(j = items-1;j >= 0;j--){ - if(Key >Arr[j]) { inARR[J+1] =Arr[j]; - } to Else{ + Break; - } the } *ARR[J+1] =key; $items++;Panax Notoginseng } - } the + Public LongRemove () { A returnarr[--items]; the } + - Public BooleanIsEmpty () { $ returnItems = = 0; $ } - - Public BooleanIsfull () { the returnItems = =maxSize; - }Wuyi the Public Longgetpeekmin () { - returnArr[items-1]; Wu } - About Public voidDiaplaypq () { $ for(inti = Items-1;i >= 0;i--){ -System.out.print (Arr[i] + ""); - } - System.out.println (); A } +}
Priority queue (storage structure array)--java implementation