com.atguigu.queue Package; Import java.util.Scanner; public class CircleArrayQueueDemo { public static void main (String [] args) { // Test a System.out.println ( "test case annular array of analog queue ~ ~ "); // Create a circular queue CircleArray queue = new CircleArray (4) ; // description set 4, which is the maximum effective data queue. 3 char Key = ''; // receiving user input Scanner scanner = new Scanner ( the System.in); // Boolean = Loop to true; // output a menu the while (Loop) { System.out.println ( "S (Show): display queue"); System.out.println ( "E (Exit) : exit the program "); System.out.println (" a (the Add): add data to a queue "); System.out.println (" G (GET): data taken from the queue "); System.out.println ( "h (head): See data queue head"); the System.out.println(e.getMessage()); key = scanner.next () charAt (0 ); // receiving a character. Switch (Key) { Case 'S': queue.showQueue (); BREAK; Case 'A': System.out.println ( "the output of a number "); int value = scanner.nextInt (); queue.addQueue (value); BREAK; Case 'G': // extracted data the try { int queue.getQueue RES = (); System.out.printf (" removed the data D% \ n-", RES); } the catch (Exception E) { // the TODO: Exception handle } BREAK; Case 'H':// Check data queue head try { int queue.headQueue RES = (); System.out.printf ( "head of the queue data is D% \ n-", RES); } the catch (Exception E) { // the TODO: Exception handle the System.out. the println (e.getMessage ()); } BREAK; Case 'E': // exit scanner.close (); Loop = to false; BREAK; default: BREAK; } } System.out.println ( "program exits ~~" ); } } class CircleArray { Private int the maxSize; // represents the maximum capacity of the array // make a front adjustment variable meanings: front points to the first element of the queue, i.e. ARR [front] is the first element of the queue // front of the initial value = 0 int Front Private; // make the meaning of a rear adjustment variable: a rear pointing to the position after the last element of the queue because the empty space desired as a convention. @ rear initial value of 0 = Private rear int; // 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; } // 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 the rear shift, 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 throw new RuntimeException ( "queue is empty, the data can not take"); } // here is directed to parse out the front of a queue element // 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; } // iterate // show all data queue void showQueue public () { IF (isEmpty ()) { System.out.println ( "queue is empty, no data ~ ~"); return; } // ideas: traversing the Front, the number of elements traversed // movable brains for (int I = Front; I <Front + size (); I ++) { System.out.printf ( "ARR [D%] D =% \ n-", the maxSize I%, ARR [I the maxSize%]); } } // determine the number of valid data in the current queue public int size () { // REAR = 2 // = Front. 1 // = the maxSize. 3 return (the maxSize REAR + - Front) the maxSize%; } // display the queue the header data, note data is not taken public int headQueue () { // determines if (isEmpty ()) { throw new RuntimeException ( "queue is empty, no data ~ ~"); } return ARR [Front]; } }
Use an array to queue
Guess you like
Origin blog.csdn.net/yz18931904/article/details/104177179
Recommended
Ranking