Java data structure and algorithm value priority queue

Source: Internet
Author: User

One, priority queue

What is a priority queue: A priority queue is a data structure that is more commonly used than stacks and queues. In the priority queue, the data items are ordered by the value of the keyword. When data items are inserted into a queue, they are inserted sequentially into the appropriate position to guarantee the order of the queues.

Examples of life, suppose you have a number of pieces, the files you most need to process are placed at the top of all messages, and if the files you don't need to deal with are placed below.

Reference code:

 PackageEdu.structure.queue; Public classPriorityq {Private intmaxSize; Private int[] quearray; Private intNelmes;  PublicPRIORITYQ (intmaxSize) {         This. maxSize =maxSize; Quearray=New int[MaxSize]; Nelmes= 0; }     Public voidInsertintvalue) {        if(Nelmes = = 0) {Quearray[nelmes++] =value; } Else {            inti;  for(i = nElmes-1; I >= 0; i--) {                if(Value >Quearray[i]) {Quearray[i+ 1] =Quearray[i]; } Else {                     Break; }} quearray[i+ 1] =value; Nelmes++; }    }     Public intRemove () {returnquearray[--Nelmes]; }     Public BooleanIsfull () {return(Nelmes = =maxSize); }     Public BooleanIsEmpty () {return(Nelmes = = 0); }}

  

Second, the test code

     Public Static void Main (string[] args) {        priorityq priorityq=new priorityq (ten);        Priorityq.insert (ten);        Priorityq.insert (5);        Priorityq.insert (a);        Priorityq.insert (a);          while (! Priorityq.isempty ()) {            System.out.println (Priorityq.remove ());        }    }

Java data structure and algorithm value priority queue

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.