An array of structures and algorithms --2 surround queue

Surround queue

Queue meaning:

A queue is an ordered list, can be an array or a linked list is implemented
following the first-in first-out principle, that is first stored in the data queue first removed after deposit to remove

Analog queue array idea 
queue itself is an ordered list, an array structure used to store data queues, the maxSize is the maximum capacity of the queue 
because the output queue, is input respectively from the rear end to handle, thus requiring two front and rear variables were recorded before and after the end of the queue of the subscripts, 
front will change as the output data, and the rear is input as data changes
Analog circular queue array idea 
meaning 1front be adjusted variable, a pointer to the first element on the front of the queue, i.e. ARR [front] is the first element of the queue, the front initial value = 0 
the meanings of the variables 2rear be adjusted, a position pointing to the last element of the real queue, an empty space because it is desirable to make an appointment, a rear initial value = 0 
3 when the queue is full, with the proviso that [rear + . 1 ] ==% the maxSize Front 
. 4 when the queue is empty the condition REAR == Front 
. 5 the number of valid data queue [REAR + the maxSize-Front]% the maxSize

Question 1: Why get the value of% maxSize

 

 Since the array is an annular array, when the end of the array is full, but the beginning of the array data extraction, then data can be stored to the space. So use% maxSize

Is the first value in the array, rear the array represents the last value represents the front, an annular array of coordinates at this time does not mean that it belongs to the beginning or end of

 

Question 2: rear == front and empty queue full queue [rear + 1]% maxSize == front different conditions:

 

 As illustrated in accordance with, real == front there are two cases, empty or full. We modify its conditions, to retain an element of space. In other words, when the queue is full, there is a free cell array.

 As shown below, we believe that this queue is full, that is to say, we do not allow the drawing occurs.

 

example

 

com.ujiuye.queue Package; 

Import java.util.Scanner; 

public  class CircleArrayQueue {
     public  static  void main (String [] args) { 
        . the System OUT .println ( " Test Model cases Queue " );
         // array length is 3, However, one of which is empty, you can put a maximum of two data 
        CircleArray Queue = new new CircleArray ( . 3 ); 
        Scanner SC = new new Scanner (the System. in );
         char Key = '  ' ; 
        Boolean in Flag = to true ;
        the while (In Flag) { 
            . the System OUT .println ( " S [Show], display queue " ); 
            . the System OUT .println ( " E [Exit], delete queue " ); 
            . the System OUT .println ( " A [the Add] adding queue " ); 
            . the System OUT .println ( " G [GET], acquires data queue " ); 
            . the System OUT .println ( " H [head], see column header data " ); 
            . the System OUT .println ( " ==================" ); 
            . The System OUT .println ( " Please enter your operation to be performed (or English acronym Please reply!) " ); 
            Key = sc.next () the charAt (. 0 );
             Switch (Key) {
             Case  ' S ' : 
                queue.showQueue (); 
                BREAK ;
             Case  ' a ' : 
                the System. OUT .println ( " add a value " );
                 int n-= sc.nextInt (); 
                queue.addQueue (n-); 
                BREAK;
            case 'g':
                try {
                    int res = queue.getQueue();
                    System.out.printf("取出的数值是%d\n",res);
                }catch(Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'h':
                try {
                    int res = queue.headQueue();
                    System.out.printf("队列头数值是%d\n",res);
                }catch(Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'e':
                sc.close();
                flag = false;
                break;
            default:
                break;
            }
        }
        
    }
}
class{CircleArray
     // represents the maximum capacity of the array 
    Private  int the maxSize;
     // first element points to the queue, arr [front] is the first element of the queue, the initial value 0 
    Private  int Front;
     // points to the last queue a position of a rear element, an empty space because it is desirable to make an appointment, the initial value of the rear 0 = 
    Private  int rear;
     // used to store data, analog queue 
    Private  int [] ARR;
     public CircleArray ( int the maxSize) {
         the this . = the maxSize the maxSize; 
        ARR = new new  int [the maxSize]; 
    } 
    // when the queue is full, with the proviso that [REAR +. 1] == Front% the maxSize 
    publicisFull Boolean () {
         return (+ REAR . 1 ) the maxSize% == Front; 
    } 
    // if the queue is empty condition rear == front 
    public Boolean isEmpty () {
         return REAR == Front; 
    } 
    // to add data queue 
    public  void addQueue ( int n-) {
         // first determine whether the queue is full 
        iF (isFull ()) { 
            the System. OUT .println ( " queue is full, line " );
             return ; 
        } 
        // because a rear element represents the last a position, certainly empty, so you can not go directly into the plus 1 
        arr [REAR] = n-;
         // the rear after the shift, consider surrounding the problem, so a modulo 
        rear = (+ rear . 1 )% the maxSize; 
    } 
    // get the data from the queue 
    public  int getQueue () {
         // first determine whether the queue is empty 
        IF (isEmpty ()) {
             // Throws not return 
            the throw  new new a RuntimeException ( " queue is empty, no value " ); 
        } 
        // get the first value in the queue 
        int value = ARR [Front];
         // this case after the front should move, again to ensure that the value is the first to get a value, taking into account the issues surrounding the 
        front = (front + . 1 )% the maxSize;
         returnvalue; 
    }
    // all the values in the queue 
    public  void showQueue () {
         // first determine whether the queue is empty 
        IF (isEmpty ()) {
             the throw  new new a RuntimeException ( " queue is empty, no value " ); 
        } 
        // iterate from a front traversing the first value 
        for ( int I = front; I <size () + front; I ++ ) { 
            . the System OUT .printf ( " ARR [D%] D =% \ n- " , the maxSize I%, ARR [ % I the maxSize]); 
        } 
    } 
    // determine the number of data current listing 
    public  int returnsize () {
         (+ REAR the maxSize-Front)% the maxSize; 
    } 
    // display the data queue head 
    public  int headQueue () {
         IF (isEmpty ()) {
             the throw  new new a RuntimeException ( " queue is empty, no value " ); 
        } 
        return ARR [Front]; 
    } 
}

 

。。

Guess you like

Origin www.cnblogs.com/bai3535/p/12094059.html