A queue is an important data structure. Its main application is the queuing of resources (such as printers). It should be noted that cyclic data should be used to store data.
Call code
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 ());
}
Java files
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 ();
}
}
A queue is an important data structure and has an important application in queuing theory and algorithm design. In fact, a queue is also a kind of linked list. It only allows a table to be output (dequeue) at the beginning of the table ), at the end of the table (enqueue), the above is the java implementation of the queue.