/*--------------definition and test code for the queue class----------------*/functionQueue () { This. DataStore = []; This. Enqueue =Enqueue; This. dequeue =dequeue; This. Front =Front; This. Back =Back ; This. toString =toString; This. Empty =empty;}//The queue is to add an element at the end of the arrayfunctionEnqueue (Element) { This. Datastore.push (Element);}//out of the team, judge the priority Delete, note here is the array of splice method, not the slice methodfunctiondequeue () {varPriority = This. datastore[0].code; varFromIndex = 0; for(varI=1; i< This. datastore.length; ++i) {if( This. Datastore[i].code <Priority ) {FromIndex=i; } } return This. Datastore.splice (FromIndex, 1);}//Take out the first element of the arrayfunctionFront () {return This. datastore[0];}//remove the last element of the arrayfunctionBack () {return This. datastore[ This. datastore.length-1];}functiontoString () {varRetstr = ""; for(vari=0; i< This. datastore.length; ++i) {retstr+ = "Patient:" + This. Datastore[i].name + "Priority:" + This. Datastore[i].code + "<br>" } returnretstr;}//determine if the array is emptyfunctionempty () {if( This. Datastore.length = = 0){ return true; }Else{ return false; } }//returns the number of elements in an arrayfunctioncount () {return This. Datastore.length;}/*----------------Base Sort-----------------*/functionPatient (name, code) { This. Name =name; This. Code =Code;}varp =NewPatient (' Smith ', 5);vared =NewQueue (); Ed.enqueue (p);p=NewPatient (' Jones ', 4); Ed.enqueue (p);p=NewPatient (' Fehrendbach ', 6); Ed.enqueue (p);p=NewPatient (' Brown ', 1); Ed.enqueue (p);p=NewPatient (' Ingram ', 1); Ed.enqueue (P);d Ocument.write (ed.tostring ());varseen =ed.dequeue ();d Ocument.write (' <br> ');d Ocument.write ("Service Patient:" + seen[0].name);d ocument.write (' <br> ');d Ocument.write (ed.tostring ()); seen=ed.dequeue ();d Ocument.write (' <br> ');d Ocument.write ("Service Patient:" + seen[0].name);d ocument.write (' <br> ');d Ocument.write (ed.tostring ()); seen=ed.dequeue ();d Ocument.write (' <br> ');d Ocument.write ("Service Patient:" + seen[0].name);d ocument.write (' <br> ');d Ocument.write (ed.tostring ());
The output is:
JavaScript implementation Priority queue