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 ());}}