Queue (storage structure array)--java implementation

Source: Internet
Author: User

1 /*queue: In fact, it is also a linear table with restricted operation2 * Features: FIFO3 * Team Tail Hands: Responsible for elements of the incoming team4 * Team Head Pointer: responsible for the elements of the team5 * Note: Normal team-easy to waste space, the general queue is most used is the circular queue-the pointer around6 * How queues are implemented: arrays/linked Lists7 * The queue is empty and full:8 * * According to the number of elements in the queue9 * * According to the relationship between team head and tail pointerTen * There is a double-ended queue: Each end can be inserted and deleted, its deformation can be the operation of the limited double-ended queue One * Queue application: In fact, many real-world rules are in line with this idea A  * */ -  Public classMyqueue { -     Private intItems//number of queue elements the     Private Long[] arr;//Storing Arrays -     Private intFront//Team head Pointer -     Private intRear//Team Tail Hands -     Private intMaxSize;//the length of the array +      -      PublicMyqueue (ints) { +MaxSize =s; Aarr =New Long[maxSize]; atFront = 0; -Rear =-1; -Items = 0; -     } -      -     //Enter the team--first add to take in      Public voidInsertLongkey) { -         if(Rear = = MaxSize-1){ toRear =-1; +         } -Arr[++rear] =key; theitems++; *     } $     Panax Notoginseng     //out of the squad--first in the plus 1 . -      Public LongRemove () { the         Longnum = arr[front++]; +         if(Front = =maxSize) { AFront = 0; the         } +items--; -         returnnum; $     } $      -     //Get Team head -      Public LongGetfront () { the         returnArr[front]; -     }Wuyi      the     //empty sentence -      Public BooleanIsEmpty () { Wu         returnItems = = 0; -     } About      $      Public BooleanisEmpty1 () { -         return(Rear + 1 = = Front | | (front + maxSize-1 = =rear)); -     } -      A      +      the     //Full sentence -      Public BooleanIsfull () { $         returnRear = =maxSize; the     } the      the      Public BooleanisFull1 () { the         return(Rear + 2 = = Front | | (front + maxSize-2 = =rear)); -     } in      the     //get the number of queue elements the      Public intsize () { About         returnitems; the     } the      the      Public intsize1 () { +         if(Rear >=front) { -             returnRear-front + 1; the         }Bayi         Else{ the             return(Maxsize-front) + (rear + 1); the         } -     } -      the     //Display Queue the      Public voidDisplayqueue () { the          for(inti = Front;i < rear;i++){ theSystem.out.print (Arr[i] + ""); -         } the System.out.println (); the     } the     94      the}

Queue (storage structure array)--java implementation

Related Article

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.