Recently looking at data structures, queues are an important element in data structures.
Definition: A data structure is a collection of elements that have one or more specific relationships to each other.
The queue is divided into ordinary queue and ring queue, the ring queue is more efficient than the normal queue (the normal queue is prone to waste of memory, the time efficiency will be reduced, mainly reflected in the deletion operation of the queue)
Following the implementation of the queue in Java, for reference only
Packagedemo;//Ring Queue Public classQueuedemo {//Create a queue PublicQueuedemo (intnum) {m_iqueuecapacity=num; M_pqueue=NewObject[num]; Clearqueue (); } //Destroy Queue Public voidDestryqueue () {M_pqueue=NULL; } //Empty Queue Public voidClearqueue () {M_ihead=0; M_itail=0; M_iqueue=0; } //determine if the queue is empty Public BooleanIsempotyqueue () {if(m_iqueue==0){ return true; } return false; } //determine if the queue is full Public BooleanQueuefull () {if(m_iqueue==m_iqueuecapacity) { return true; } return false; } //Queue Length Public intqueuelength () {returnM_iqueue; } //new Element Public BooleanEnQueue (Object element) {if(Queuefull ()) {return false; } M_pqueue[m_itail]=element; M_itail++; M_itail=m_itail%m_iqueuecapacity; M_iqueue++; return true; } //Delete Element PublicObject DeQueue () {if(Isempotyqueue ()) {return NULL; } Object Element=M_pqueue[m_ihead]; M_ihead++; M_ihead=m_ihead%m_iqueuecapacity; M_iqueue--; returnelement; } //Traverse Queue Public voidQueuetraverse () { for(inti=m_ihead;i<m_iqueue+m_ihead;i++) {System.out.println (m_pqueue[i%m_iqueuecapacity]); } } PrivateObject[] M_pqueue;//Queue Private intM_iqueue;//number of queue elements Private intm_iqueuecapacity;//Queue Array Capacity Private intM_ihead;//rival Private intM_itail;//End of Team Public Static voidMain (string[] args) {Queuedemo demo=NewQueuedemo (5); Demo. EnQueue ("AAA"); Demo. EnQueue ("BBB"); Demo. EnQueue ("CCC"); Demo. EnQueue ("DDD"); Demo. DeQueue (); Demo. DeQueue (); Demo. EnQueue ("FFFF"); Demo. EnQueue ("VVVV"); Demo.queuetraverse (); }}
View Code
A circular queue in the Java implementation data structure