What is Java Data Structure queue?

Source: Internet
Author: User

Without the basis of the java data structure, how can we optimize the performance of Android applications? In real life, queues are widely used, such as queuing for shopping and printing articles, all follow the principle of queue first-in-first-out. The queue Queue is explained in our Handel low.thread chapter. Today we will focus on the nature of the queue.

Queue is also a linear table with limited operations. It can be inserted only at one end of the table and deleted at the other end. The end that can be deleted is called the front and the end that can be inserted is called the rear ).

Concepts related to sequential queue <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgoKPHA + ICAgICAgICA/hybrid + NDQyb6z/bLZ1/hybrid + CgoKPHA + ICAgICAgICA/ttPB0L/hybrid + http://www.2cto.com/uploadfile/Collfiles/20140527/20140527093224232.jpg/alt = "alt "= "\">

Therefore, we have designed a circular queue to think of the vector space as a starting and ending ring and call it a circular vector. Note that the condition when the cyclic queue is full is (rear + 1) % MaxSize = front. To avoid conflicts with the empty queue condition, a space is reserved.


Next we will look at how to implement the above circular queue.

Package c; public class SeqQueue
 
  
Implements QQueue
  
   
{Private Object [] element; private int front, rear; public SeqQueue (int size) {// TODO Auto-generated constructor stubthis. element = new Object [Math. abs (size)]; this. front = this. rear = 0;} // construct an empty method. The default size is 64 public SeqQueue () {// TODO Auto-generated constructor stubthis (64 );} // determine whether the queue is empty @ Overridepublic boolean isEmpty () {// TODO Auto-generated method stubreturn this. front = this. rear;} // queue entry @ Override Public void enqueue (T x) {// TODO Auto-generated method stubif (x = null) return; // how the queue is full, re-apply for a space of two times if (this. front = (this. rear + 1) % element. length) {Object [] temp = this. element; this. element = new Object [temp. length * 2]; int I = this. front; int j = 0; while (I! = This. rear) {this. element [I] = temp [I]; I = (I + 1) % temp. length; j ++;} this. front = 0; this. rear = j;} this. element [this. rear] = x; this. rear = (this. rear + 1) % element. length;} // outqueue operation @ Overridepublic T dequeue () {// TODO Auto-generated method stubif (isEmpty () return null; @ SuppressWarnings ("unchecked ") T temp = (T) this. element [this. front]; this. front = (this. front + 1) % this. element. length; return tem P ;}// print the public String toString () {String s = "("; if (! IsEmpty () {s = s + this. element [this. front]. toString (); int I = (this. front + 1) % this. element. length; while (I! = This. rear) {s = s + this. element [I]. toString (); I = (I + 1) % this. element. length ;}} return s + ")" ;}// test the public static void main (String args []) {SeqQueue
   
    
Queue = new SeqQueue
    
     
(64); for (int I = 0; I <10; I ++) {queue. enqueue ("a" + I);} System. out. print (queue. toString ());}}
    
   
  
 


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.