Java queue implementation method

Source: Internet
Author: User

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.

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.