Queue concept
A queue is a linear table that is qualified to be inserted only at one end and deleted at the other end. The one end of the allowed delete is called the team Head (front), the one end of the insert is called the tail of the queue (rear), and the queues without elements are called "empty Queues".
The queue has an advanced first-out (FIFO) feature.
Problems with normal sequential queues
In a normal sequential queue, the queued operation is to move the tail pointer rear to the right one unit, and then assign the value of the element to the rear unit. When the team is out, the head pointer front a unit. Such a situation may occur after a certain number of enqueue and out-of-band operations like this:
Tail pointer rear has been pointed to the last element of the array, that is, rear==maxlen-1, at this time if the front part of the array may have a lot of idle space, that is, this overflow is not really no storage space available, it is called this overflow phenomenon "false overflow." Obviously, the problem of this false overflow must be solved, otherwise the sequential queue will not have much use value.
Loop queue
The storage structure of the circular queue, with the same head and tail pointers as the normal sequential queue. The difference is simply to treat the queue as a "ring structure", i.e. data[0] as a unit immediately following Data[maxlen-1], and for the adjacent element, the first becomes a ring. The structure is as follows: (from: Wikipedia)
Code implementation
Global variables: Defining queue Lengths
Static int MaxLen;
Implementation of the basic data structure of the cyclic queue:
Static class myqueue{ int front; int rear; int queuelist[]; Public Myqueue () { // TODO auto-generated constructor stub queuelist= New int [MaxLen]; Front=0; Rear=0; }}
Empty function
Public Boolean IsEmpty () { if(front==rear) { returntrue; } return false ; }
Full function
Public Boolean Isfull () { if(((rear+1)%maxlen) = =Front) {returntrue; } Else { returnfalse; } }
Take the team head element
Public void queuefront (int getfront) { if(isEmpty () = =false) { Getfront=queuelist[(front+1)%maxlen]; } Else { System.out.println ("Error:queue is Empty"); return ; } }
Team
Public void enQueue (int endata) { if(isfull () = =false) { rear = (rear+1)%maxlen; this. queuelist[rear]=endata; } Else { System.out.println ("Error:queue is full"); return ; } }
Out Team
Public void Outqueue () { if(isEmpty () = =false) { Front= (front+1)%maxlen; } Else { System.out.println ("Error:queue is Empty"); return ; } }
Implementation of the Java loop queue