JS Priority queue ordering. When you are out of the team, first identify the highest priority elements, and then follow the FIFO first-out team.

Source: Internet
Author: User

JS Priority queue ordering. When you are out of the team, first identify the highest priority elements, and then follow the FIFO first-out team.

/** Priority Queue * When out of the team, first identify the highest priority elements, and then follow the FIFO first-out team. * */functionQueue () { This. DataStore = [];//an array of storage queues, initialized to null     This. enqueue = Enqueue;//add an element to the tail of the queue     This. dequeue = dequeue;//when you are out of the team, first identify the highest priority elements, and then follow the FIFO first-out team.      This. Thefront = Thefront;//read the elements of the first team     This. back = back;//the element that takes the tail of the team     This. tostrings = tostrings;//Show all elements in a queue     This. empty = Empty;//determine if the queue is empty}/*define the object that stores the queue element first*/functionPatient (name,code) { This. name = name;//code is an integer that indicates the priority of the patient     This. Code =Code;}functionEnqueue (Element) { This. Datastore.push (Element);}functiondequeue () {varMinindex = 0; varPriority = This. datastore[0].code;  for(vari = 1;i< This. datastore.length;i++){        if( This. Datastore[i].code <Priority ) { Priority= This. Datastore[i].code; Minindex=i; }    }    return  This. Datastore.splice (minindex,1);}functionThefront () {return  This. datastore[0];}functionBack () {return  This. datastore[ This. datastore.length-1];}functiontostrings () {return  This. DataStore;}functionempty () {if( This. Datastore.length = = 0){       return true; }Else{        return false; }}/*implementation of the priority queue*/vared =NewQueue ();varp =NewPatient ("AA", 5); Ed.enqueue (p);varp =NewPatient ("BB", 4); Ed.enqueue (p);varp =NewPatient ("CC", 3); Ed.enqueue (p);varp =NewPatient ("DD", 3); Ed.enqueue (p);varp =NewPatient ("ee", 1); Ed.enqueue (P); Console.log (Ed.tostrings ()); Console.log (Ed.dequeue ()) ;//[Patient {name: ' ee ', code:1}]Console.log (Ed.dequeue ());//[Patient {name: ' CC ', code:3}]Console.log (Ed.dequeue ());//[Patient {name: ' DD ', code:3}]

JS Priority queue ordering. When you are out of the team, first identify the highest priority elements, and then follow the FIFO first-out team.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.