1. Use the queue: Square Dance partner assignment problem
As we mentioned before, we often use queues to simulate people queuing. Below we use the queue to simulate the person who jumps the square dance. When men and women came to the dance floor, they formed two teams according to their sex. When there is space on the dance floor, choose the first person in two queues to form a partner. The people behind them each move forward, becoming the new team head. When a pair of partners enters the dance floor, the host will shout out their names. When a pair of partners out of the dance floor, and the two rows of the team there is no one, the host will also tell you this situation. To simulate this, we stored the names of the men and women who danced the square dance in a text file: The following is the implementation of the program code:
<script type= "Text/javascript" >functionQueue () { This. DataStore = []; This. Enqueue =Enqueue; This. dequeue =dequeue; This. Front =Front; This. Back =Back ; This. toString =toString; This. Empty =empty; This. Count =count;}/** * Add an element to the end of the team*/functionEnqueue (Element) { This. Datastore.push (Element);}/** * Delete the elements of the first team:*/functiondequeue () { This. Datastore.shift ();}/** * Read the elements of the first team:*/functionFront () {return This. datastore[0];}/** * Read the element at the end of the queue:*/functionBack () {return This. datastore[ This. datastore.length-1];}/** * Show all elements in the queue*/functiontoString () {varRetstr = ""; for(vari = 0; I < This. datastore.length; ++i) {retstr+= This. datastore[i] + "\ n"; } returnretstr;}/** * Determine if the queue is empty*/functionempty () {if( This. Datastore.length = = 0){ return true; }Else{ return false; }}/** * Show how many elements are in a queue*/functioncount () {return This. Datastore.length;}//=================================== using the queue class =============================================/** * Each dancer's information is stored in a dancer object*/functionDancer (name, sex) { This. Name =name; This. Sex =sex;}/** * The dancer information is read from the file to the program * TRIM () function removes space after each line of String * The dancers are added to different queues according to gender*/functiongetdancers (males, females) {varnames = Read ("Dancers.txt"). Split ("\ n")); for(vari = 0; i < names.length; ++i) {names[i]=Names[i].trim (); } for(vari = 0; i < names.length; ++i) {varDancer = Names[i].split (""); varSex = dancer[0]; varName = Dancer[1]; if(Sex = "F") {Females.enqueue (NewDancer (name, sex)); }Else{males.enqueue (NewDancer (name, sex)); } }}/** * Male and female partners are formed and the results of the pairing are announced*/functionDance (males, females) {Console.log ("The dance Partners is: \ n"); while(!females.empty () &&!Males.empty ()) { person=Females.dequeue (); Console.log ("Female Dancer is:" +person.name); person=Males.dequeue (); Console.log ("And the male dancer is:" +person.name); }}/** * Test procedure:*/varMaledancers =NewQueue ();varFemaledancers =NewQueue (); Getdancers (Maledancers, femaledancers);d ance (maledancers, femaledancers);if(!Femaledancers.empty ()) {print (Femaledancers.front (). Name+ "is waiting to dance.");}if(!Maledancers.empty ()) {print (Maledancers.front (). Name+ "is waiting to dance.");}//show the number of people waiting to danceif(Maledancers.count () > 0) {print ("There is" + maledancers.count () + "male dancers waiting to dance.");}if(Femaledancers.count () > 0) {print ("There is" + femaledancers.count () + "female dancers waiting to dance.");}</script>
View Code
2.
Data structure and algorithm JavaScript description--Using queues