A circular queue in the Java implementation data structure

Source: Internet
Author: User

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

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.