1. Queue: Unlike the case in the stack, the data items in the queue do not always start with the array subscript 0, and after removing a data item, the team head pointer points to the higher subscript data item, which features: first in, first out
2. Illustrations
3. Implementation code for the queue:
3.1.queue.java
1 PackageCom.cn.queue;2 /**3 * Data structure of the queue implementation4 * @authorAdministrator5 *6 */7 Public classQueue {8 Private intmaxsize;9 Private Long[] queuearray;Ten Private intFront; One Private intRear; A Private intNitems; - PublicQueue (ints) { -MaxSize =s; theQueuearray =New Long[maxsize]; -Front = 0; -Rear =-1; -Nitems = 0; + } - Public voidInsertLongj) { + if(Rear = = Maxsize-1) ARear =-1; atqueuearray[++ rear] =J; -Nitems + +; - } - Public LongRemove () { - LongTemp = Queuearray[front + +]; - if(Front = =maxsize) inFront = 0; -Nitems--; to returntemp; + } - Public LongPeekfront () { the returnQueuearray[front]; * } $ Public BooleanIsEmpty () {Panax Notoginseng return(Nitems = = 0); - } the Public BooleanIsfull () { + return(Nitems = =maxsize); A } the Public intsize () { + returnNitems; - } $ $}
3.2.queuetest.java
1 PackageCom.cn.queue;2 3 Public classQueuetest {4 Public Static voidMain (string[] args) {5Queue q =NewQueue (100);6Q.insert (100);7Q.insert (200);8Q.insert (300);9 while(Q.size ()!=0){TenSystem.out.print (Q.remove () + ""); One } ASystem.out.println (""); - System.out.println (Q.isempty ()); - } the}
4. Queue insertion and deletion time complexity is the same as the stack, both O (1)
5. Priority queue: The priority queue is a more dedicated data structure than stacks and queues, he has a team head and tail, and also removes data items from the team head, but in the priority queue, the data items are ordered by the value of the keyword, so that the smallest item of the keyword is always on the team head, and the largest is at the end Insert operations are inserted in the proper order to ensure the order of the queues. In addition to fast access to data items with minimum critical values, priority queues must also implement very fast insert data items, whereby priority queues are typically implemented using a data structure called heap. This program uses the array implementation temporarily, the implementation of the insertion is relatively slow, applicable to the small amount of data, and the speed requirements are not high scene.
6. Implementation of the priority queue:
6.1.priorityq.java
1 PackageCom.cn.queue;2 /**3 * Implementation code of the priority queue4 * @authorAdministrator5 *6 */7 Public classPriorityq {8 Private intmaxsize;9 Private Long[] queuearray;Ten Private intNitems; One PublicPRIORITYQ (ints) { AMaxSize =s; -Queuearray =New Long[maxsize]; -Nitems = 0; the } - Public voidInsertLongItem) { - intK; - if(Nitems = = 0) +Queuearray[nitems + +] =item; - Else{ + for(k = nitems-1;k >= 0;k--){ A if(Item >Queuearray[k]) atQueuearray[k + 1] =Queuearray[k]; - Else - Break; - } -Queuearray[k + 1] =item; -Nitems + +; in } - } to Public LongRemove () { + returnqueuearray[--Nitems]; - } the Public Longpeekmin () { * returnQueuearray[nitems-1]; $ }Panax Notoginseng Public BooleanIsEmpty () { - return(Nitems = = 0); the } + Public BooleanIsfull () { A return(Nitems = =maxsize); the } +}
6.2 Illustrations
7. Efficiency of priority queue: insert operation time complexity bit O (N), delete operation time complexity is O (1), subsequent heap data structure will improve him.
Java Data structure----queue, priority queue