Source Code :
of the priority queue
Package COM. priorityquence; <br/>/** <br/> * @ author kinglong1203 2011.2.20 <br/> */<br/> public class maxpriorityquence {</P> <p> private int size; <br/> private int quencesize; <br/> private int [] array; <br/> Public maxpriorityquence () {<br/> size = 50; <br/> quencesize = 0; <br/> array = new int [size]; <br/>}</P> <p> Public maxpriorityquence (INT size) {<br/> super (); <br/> This. size = siz E; <br/> This. quencesize = 0; <br/> array = new int [size]; <br/>}< br/> Public int getsize () {<br/> return size; <br/>}< br/> Public void setsize (INT size) {<br/> This. size = size; <br/>}< br/> Public int getquencesize () {<br/> return quencesize; <br/>}< br/> Public void setquencesize (INT quencesize) {<br/> This. quencesize = quencesize; <br/>}< br/> Public Boolean isfull () {<br/> If (quencesize = size) {<B R/> return true; <br/>}< br/> return false; <br/>}</P> <p> Public Boolean isempty () {<br/> If (quencesize <1) {<br/> return true; <br/>}< br/> return false; <br/>}</P> <p> Public void maxpriorityincrease (int I, int X) {<br/> If (x <array [I-1]) {<br/> system. out. println ("this value is smaller than the existing value and cannot be increased! "); <Br/>}< br/> I = I-1; <br/> array [I] = X; <br/> while (I> 0 & array [(I-1)/2] <array [I]) {<br/> int temp = array [I]; <br/> array [I] = array [(I-1)/2]; <br/> array [(I-1)/2] = temp; <br/> I = (I-1)/2; <br/>}</P> <p> Public void maxpriorityinsert (INT X) {<br/> If (! Isfull () {<br/> array [quencesize] = integer. min_value; <br/> quencesize ++; <br/> maxpriorityincrease (quencesize, x); </P> <p >}else {<br/> system. out. println ("the queue is full! "); <Br/>}< br/> // gets the element with the highest priority. <br/> Public int maximum () {<br/> buildmaxheap (); <br/> return array [0]; <br/>}< br/> // extracts the element with the highest priority, delete this element in the queue <br/> Public int extractmax () {<br/> If (quencesize <1) {<br/> system. err. println ("the queue is empty! "); <Br/> return-1; <br/>}</P> <p> quencesize --; <br/> int max = array [0]; <br/> array [0] = array [quencesize]; <br/> maxpriority (1); <br/> return Max; <br/>}</P> <p> // create the maximum heap <br/> private void buildmaxheap () {<br/> for (INT I = quencesize/2; I >= 2; I --) {<br/> maxpriority (I ); <br/>}< br/> // change from I to Max heap <br/> private void maxpriority (int I) {<br/> int S = I-1; <br/> for (Int J = 2 * s + 1; j <quencesize; j = J * 2 + 1) {<br/> If (j <quenceSize-1 & array [J] <array [J + 1]) <br/> J ++; <br/> If (array [s]> = array [J]) <br/> break; <br/> int temp = array [s]; <br/> array [s] = array [J]; <br/> array [J] = temp; <br/> S = J; <br/>}</P> <p >}< br/> // print the queue <br/> Public void printquence () {<br/> for (INT I = 0; I <quencesize; I ++) {<br/> system. out. print ("" + array [I]); <br/>}< br/> system. out. println (""); <br/>}< br/>
TestCode:
Package COM. priorityquence; <br/>/** <br/> * @ author kinglong1203 2011.2.20 <br/> */<br/> public class maxpriorityquencetest {<br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> maxpriorityquence maxpriority = new maxpriorityquence (); </P> <p> maxpriority. maxpriorityinsert (15); <br/> maxpriority. maxpriorityinsert (13); <br/> maxpriority. maxpriorityinsert (9); <br/> maxpriority. maxpriorityinsert (5); <br/> maxpriority. maxpriorityinsert (12); <br/> maxpriority. maxpriorityinsert (8); <br/> maxpriority. maxpriorityinsert (7); <br/> maxpriority. maxpriorityinsert (4); <br/> maxpriority. maxpriorityinsert (0); <br/> maxpriority. maxpriorityinsert (6); <br/> maxpriority. maxpriorityinsert (2); <br/> maxpriority. maxpriorityinsert (1); </P> <p> system. out. println ("the highest priority is:" + maxpriority. maximum (); <br/> maxpriority. printquence (); </P> <p> maxpriority. maxpriorityinsert (10); <br/> system. out. println ("the highest priority is:" + maxpriority. maximum (); <br/> maxpriority. extractmax (); <br/> maxpriority. printquence (); <br/>}< br/>}