JAVA Implementation queue

Source: Internet
Author: User

A queue is an important data structure.AlgorithmThere are very important applications in the design. In fact, the queue is also a kind of linked list. It only allows the table to start (dequeue), and the table to end (enqueue ), below is the JAVA Implementation of the queue.

 
// Queue is an important data structure. Its main application is the queuing of resources (such as printers). It should be noted that the cyclic data should be used to store the data class queue {private int front; private int back; private int size; private object [] data; Public Queue () {DATA = new object [10];} public int getsize () {return size ;} public object dequeue () {object o = NULL; If (size> 0) {size --; O = data [Front]; data [Front] = NULL; front = (front> data. length-1 )? 0: Front + 1;} return O;} public void enqueue (Object O) {If (size> = data. length) {object [] newdata = new object [data. length * 2]; for (INT I = 0; I <data. length; ++ I) {newdata [I] = data [I];} DATA = newdata;} data [Back] = O; back ++; size ++ ;} public String tostring () {stringbuffer STR = new stringbuffer (); For (INT I = 0; I <data. length; I ++) {object o = data [I]; If (o! = NULL) {Str. append (O + "") ;}} return Str. tostring ();}}

The test method is as follows:

Public static void main (string [] ARGs) {queue = new Queue (); For (INT I = 0; I <15; I ++) {queue. enqueue ("sunzhenxing" + I);} system. out. println (Queue); system. out. println (queue. getsize (); system. out. println ("----------------------"); For (INT I = 0; I <10; I ++) {queue. dequeue ();} system. out. println (Queue); system. out. println (queue. getsize (); system. out. println ("----------------------"); For (INT I = 0; I <5; I ++) {queue. enqueue ("sunhailong" + I);} system. out. println (Queue); system. out. println (queue. getsize ());}

The output is:

commandid was reported when there were too many commandid sunzhenxing10 sunzhenxing11 sunzhenxing12 sunzhenxing13 hours
15
when there were too many sunzhenxing13 sunzhenxing14
5
timeout
sunzhenxing10 sunzhenxing11 sunzhenxing12 sunzhenxing13 sunzhenxing14 sunhailong0 sunhailong1 sunhailong2 sunhailong3 sunhailong4
10

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.