Array operation mode queue

 

   

A queue introduction

 When we pay the supermarket, the first line is always the first payment is completed (excluded jump the queue), a feature of the queue is the "first in first out, last in the post"

Feature

  • A queue is an ordered list, can be achieved through an array and a linked list
  • Follow the "first in first out, after backward-out principle"
  • FIG When a queue representing an array, data is always inserted at the bottom, at the top out data

      

 

 

 

Second, the simulation array cohort analysis

MaxSize is the length of the array, that MaxSize-1 is the index of the maximum queue, front and rear front of the queue is the index, front extracted data is changed, when the change data is added rear

When you add data:

  • First, whether the queue is full, how to determine whether full?

          Rear tail pointer increases with the addition of data, when the maximum rear point index queue is full, i.e. rear == Maxsize

  • When less than, the initial value of the tail pointer points to -1 rear, it needs to first be added before the shift (i.e., rear ++), then assign rear

When removing the data:

  • First determine whether the queue is empty?

                    When the rear == front (i.e., leftmost figure), namely empty

  • Not empty tail pointer will move the front, i.e., the operation for front ++

Third, code implementation

java
. 1  Private  int Maxsize;
 2      Private  int [] ARR;
 . 3      Private  int Front;
 . 4      Private  int REAR;
 . 5      // Construction function 
. 6      public ArrayQueue ( int arrMaxsize) {
 . 7          Maxsize = arrMaxsize;
 . 8          ARR = new new  int [Maxsize];
 . 9          -1 = Front ;
 10          REAR = -1 ;
 . 11      }
 12 is      // determines whether temporal array 
13 is      public Boolean isEmpty () {
 14         return Front == REAR;
 15      }
 16  
. 17      // determines whether the array is full 
18 is      public   Boolean isFull () {
 . 19         return REAR-== Maxsize. 1 ;
 20 is      }
 21 is  
22 is      // add data array 
23 is      public  void the Add ( int n-) {
 24          IF (isFull ()) {
 25              System.out.println ( "array is full" );
 26 is              return ;
 27          }
 28          REAR ++;
 29          ARR [REAR] = n-;
 30      }
 31 is      // delete the array data 
32      public   int Remove () {
 33 is          IF (isEmpty ()) {
 34 is              new new a RuntimeException ( "Array is empty" );
 35          }
 36          Front ++ ;
 37 [          return ARR [Front];
 38 is      }
 39  
40      // Get the data head 
41 is      public  int   getHeadDate () {
 42 is          IF (isEmpty ()) {
 43 is              new new a RuntimeException ( "empty array" );
44 is          }
 45  
46 is          return ARR [Front +. 1 ];
 47      }
 48      // displays all the data 
49      public   void allDate () {
 50          IF (isEmpty ()) {
 51 is              new new a RuntimeException ( "empty array" );
 52          }
 53          for ( int I = 0; I <Maxsize; I ++ ) {
 54 is              System.out.printf ( "ARR [D%] D =% \ n-" , I, ARR [I]);
 55          }
 56 is  
57 is      }

 

Fourth, optimization

The above code is found, the array only once, remove the empty position data can not be added, it is necessary to solve the problem introducing a cyclic queue

 

Reserving a position whether the queue is full:

 

 

 When taken out after the discovery of a queue is not full, you can continue to add:

 

 

 Code

. 1   Private  int Maxsize;
 2      Private  int ARR [];
 . 3      Private  int Front; // Default is zero 
. 4      Private  int REAR; // default 0
 . 5  
. 6      // Construction function 
. 7      public CircleArray ( int arrMaxsize) {
 . 8          Maxsize = arrMaxsize ;
 . 9          ARR = new new  int [Maxsize];
 10      }
 . 11      // determines whether temporal array 
12 is      public  Boolean isEmpty () {
13 is          return REAR == Front;
 14      }
 15  
16      // determines whether the array is full 
. 17      public   Boolean isFull () {
 18 is          return (REAR +. 1)% Maxsize == Front;
 . 19      }
 20 is  
21 is      // array data adding 
22      public  void the Add ( int n-) {
 23 is          IF (isFull ()) {
 24              System.out.println ( "array is full" );
 25              return ;
 26 is          }
 27          ARR [REAR] = n-;
 28         = REAR (REAR +. 1)% Maxsize;
 29      }
 30      // delete the data array 
31 is      public   int Remove () {
 32          IF (isEmpty ()) {
 33 is              new new a RuntimeException ( "empty array" );
 34          }
 35          // a first data value to a local variable 
36          int a = ARR [Front];
 37 [          Front = (Front +. 1)% Maxsize;
 38 is          return a;
 39      }
 40  
41 is      // Get the data head 
42 is      public  int   getHeadDate () {
 43 is          IF(isEmpty ()) {
 44 is              new new a RuntimeException ( "empty array" );
 45          }
 46 is  
47          return ARR [Front];
 48      }
 49      // displays all the data 
50      public   void allDate () {
 51 is          IF (isEmpty ()) {
 52 is              new new a RuntimeException ( "empty array" );
 53 is          }
 54 is  
55          for ( int I = Front; I <Front + size (); I ++ ) {
 56 is              System.out.printf ( "ARR [D%] =% D \ n-", I Maxsize%, ARR [I% Maxsize]);
 57 is          }
58  
59      }
 60  
61 is      // valid data length, (Maxsize-front + rear) % Maxsize When rear <When front, length (Front-MAXSIZE) + (REAR-0) 
62 is      public  int size () {
 63 is          IF (isEmpty ()) {
 64              return 0 ;
 65          }
 66          return (Front-Maxsize + REAR)% Maxsize;
 67      }

 

Guess you like

Origin www.cnblogs.com/han200113/p/11526956.html