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