Queue-java Code

Source: Internet
Author: User

 Public classQueuedemo {Private intmaxSize; Private Long[] queuearray; //The head of the queue, which is actually the end of the array    Private intheader; //The tail of the queue, which is actually the head of the array    Private intfooter; Private intNelems;  PublicQueuedemo (intsize) {MaxSize=size; Queuearray=New Long[MaxSize]; Header= 0; Footer=-1; Nelems= 0; }    //Insert queue, insert from end of team     Public voidInsertLongElement) {                if(Footer = = (maxSize-1) ) {footer=-1; } queuearray[++footer] =element; Nelems++; }    //out of the queue, that is, the end of the array     Public LongRemove () {Longtemp =Queuearray[header]; Queuearray[header]= 0; if(++header = =maxSize) {Header= 0; } nelems--; returntemp; }    //Remove Queue Header element     Public LongGetHeader () {returnQueuearray[header]; }    //Remove Queue Array     Public Long[] GetArray () {returnQueuearray; }}

The queue is first-out, can be imagined as a train into the tunnel

Priority queue:

Importjava.util.Arrays;/*** Priority Series Group * *@authororlion * @create 2015-09-12*/ Public classPriorityqdemo {Private intmaxSize; Private Long[] priorityqarray; Private intheader; Private intfooter;  PublicPriorityqdemo (intsize) {MaxSize=size; Priorityqarray=New Long[MaxSize]; Header= 0; Footer=-1; }    //Insert Priority Series Group     Public voidInsertLongElement) {                inti =footer;  while(i > 0 && priorityqarray[i] >Element) {Priorityqarray[i+ 1] =Priorityqarray[i]; I--; } priorityqarray[i+1] =element; Footer++; }    //out Queue     Public LongRemove () {Longtemp =Priorityqarray[header]; Priorityqarray[header]= 0; if(++header = =maxSize) {Header= 0; }        returntemp; }    //print queue     Public voidGetpriorityq () {System.out.print (arrays.tostring (Priorityqarray)); }}

A queue that can be understood as a certain order, and also a FIFO

Queue-java Code

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.