Java data structure of the queue

1. Scene

Bank line of case:

2. Introduction queue

A queue is an ordered list, you can use an array or a linked list to achieve.

Follow the FIFO principle :

  • First stored in the data queue, first removed.
  • After the deposit to be removed after

A schematic view of an array using analog :( queuing)

3. Implementation 1: Analog Array queue

Ideas analysis

Queue itself is an ordered list, if declared using the array of structures to store data queues, the queue array is shown below, where the maximum capacity is maxSize queue.

  • is a front position before the frontmost element forwardmost free queue []
  • is the last element of the queue rear [final] containing
  • Enqueue determines whether full, the tail pointer is moved back: rear ++, and then insert arr [rear]
  • Removed from the queue, it is determined whether the air, before the pointer moved back: front ++, then removed arr [front]
  • Analyzing conditions queue is empty: When the front == rear [empty]
  • Determine whether the queue full condition: rear == maxSize - 1] [queue is full

Code

// use the array simulation queue - a write ArrayQueue class 
class ArrayQueue {
     Private  int the maxSize; // represents the maximum capacity of the array 
    Private  int Front; // queue head 
    Private  int REAR; // end of the queue 
    Private  int [] ARR; // the data used to store data, analog queue 

    // create a queue constructor 
    public ArrayQueue ( int arrMaxSize) { 
        the maxSize = arrMaxSize; 
        ARR = new new  int [the maxSize]; 
        Front = -1; //The pointing head of the queue, the analysis points to the previous position of front head of the queue 
        REAR = -1; // pointing end of the queue, the queue points to the end of the data (i.e., the queue is the last data) 
    } 

    // whether the queue is full 
    public  Boolean isFull () {
         return REAR the maxSize == -. 1 ; 
    } 

    // whether the queue is empty 
    public  Boolean isEmpty () {
         return REAR == Front; 
    } 

    // add data to a queue 
    public  void addQueue ( int n-) {
         // Analyzing if the queue is full 
        iF (isFull ()) { 
            System.out.println ( "queue is full, the data can not be added ~" );
            return ; 
        } 
        rear ++; // After allowing rear shifting 
        ARR [rear] = n-; 
    } 

    // get the data queue, the queue 
    public  int getQueue () {
         // whether the queue empty 
        IF (isEmpty ()) {
             / / throw exceptions 
            the throw  new new a RuntimeException ( "queue is empty, the data can not take" ); 
        } 
        front ++; // the front shift 
        return ARR [front]; 

    } 

    // show all the data queues 
    public  void showQueue () {
         / / traverse 
        if(isEmpty ()) { 
            System.out.println ( "queue is empty, no data ~ ~" );
             return ; 
        } 
        for ( int I = 0; I <arr.length; I ++ ) { 
            System.out.printf ( " ARR [D%] D =% \ n-" , I, ARR [I]); 
        } 
    } 

    // display the data queue head, attention is not extracted data 
    public  int headQueue () {
         // Analyzing 
        IF (isEmpty ()) {
             the throw  new new a RuntimeException ( "queue is empty, no data ~ ~" ); 
        } 
        return ARR [Front +. 1 ]; 
    } 
}
View Code

problem appear

Currently the array can not be reused, once it is not working

Optimization mode : the array into analog circular queue

4. Simulation circular queue array

Ideas:

Meaning 1.front variables to make adjustments: front pointing to the first element of the queue, that is, arr [front] is the first element of the queue

Meaning 2.rear variables make adjustments: rear point to an element after the last element of the queue, because the empty space desired as a convention (actual capacity queue = maxSize-1, understood as being given to prevent local point beyond the array range) .

3. When the queue is full, with the proviso that: (+ REAR. 1) the maxSize% == Front [full] 

4. When the queue is empty, with the proviso that: REAR == Front [empty]

The number of valid data in the queue: (+ REAR the maxSize - Front) the maxSize%

6. enqueue determines full team, to enqueue arr [rear], then rear ++

7. When removed from the queue, an empty judging team, first out queue arr [front], then front ++

Code

class CircleArray {
     Private  int the maxSize; // represents the maximum capacity of the array
     // Front meaning make a variable adjustment: front points to the first element of the queue, i.e. arr [front] is the first element of the queue 
     // the initial value of the front 0 = 
    Private  int front; 
     // meaning of a rear adjustment variable to make: a rear pointing to the position after the last element of the queue because the empty space desired as a convention..
     // rear initial value = 0 
    Private  int REAR; // end of the queue 
    Private  int [] ARR; // this data is used to store data, queue analog 
    
    public CircleArray ( int arrMaxSize) { 
        the maxSize = arrMaxSize; 
        ARR= New new  int [the maxSize]; 
    } 
    
    // whether the queue is full 
    public  Boolean isFull () {
         return (REAR +. 1) the maxSize% == Front; 
    } 
    
    // determines whether the queue is empty 
    public  Boolean isEmpty () {
         return REAR == Front; 
    } 
    
    // add data to a queue 
    public  void addQueue ( int n-) {
         // determines whether the queue is full 
        iF (isFull ()) { 
            System.out.println ( "queue is full, the data can not be added ~" );
             return ; 
        }
        // data directly added 
        ARR [REAR] = n-;
         // After REAR move, there must be considered modulo 
        REAR = (REAR +. 1)% the maxSize; 
    } 
    
    // get the data queue, the queue 
    public  int getQueue () {
         // whether the queue empty 
        iF (isEmpty ()) {
             // throw exceptions 
            the throw  new new a RuntimeException ( "queue is empty, the data can not take" ); 
        } 
        // need to be put out of the first element in the queue points to front
         // 1. first front retained value corresponding to a temporary variable
         // 2. after the front shift, considered modulo
         // 3. temporarily stored variable return 
        int value = ARR [front];
        Front = (Front +. 1)% the maxSize;
         return value; 

    } 
    
    // show all the data queues 
    public  void showQueue () {
         // iterate 
        IF (isEmpty ()) { 
            System.out.println ( "queue is empty, no data ~ ~ " );
             return ; 
        } 
        // ideas: traversing from the front, the number of elements traversed
         // brains 
        for ( int I = Front; I <Front + size (); I ++ ) { 
            System.out.printf ( " ARR [D%] D =% \ n-", the maxSize I%, ARR [I% the maxSize]); 
        } 
    } 
    
    //Obtaining current queue number of valid data 
    public  int size () {
         // REAR = 2
         // Front. 1 =
         // the maxSize. 3 = 
        return (the maxSize REAR + - Front)% the maxSize;    
    } 
    
    // display data queue head, Note that the data is not taken 
    public  int headQueue () {
         // Analyzing 
        IF (isEmpty ()) {
             the throw  new new a RuntimeException ( "queue is empty, no data ~ ~" ); 
        } 
        return ARR [Front]; 
    } 
}
View Code

 

Guess you like

Origin www.cnblogs.com/MWCloud/p/11239320.html