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