标签:next img 输入 circle rmi test The rom private
队列特点
例如小朋友排队打针,先排队的先打针,后排队的后打针,不准插队,不准谦让!!!
1 /** 2 * 环形队列 3 * @author Haidnor 4 * @creat 2019-12-12-11:35 5 */ 6 public class CircleQueue { 7 private int front = -1; 8 private int rear = -1; 9 private int maxSize = 0; 10 private Integer[] arr; 11 12 public CircleQueue(int maxSize) { 13 this.maxSize = maxSize; 14 this.arr = new Integer[maxSize]; 15 } 16 17 /** 18 * Add data to the queue 19 * @param num 20 */ 21 public void add(int num){ 22 if(isEmpty()) { 23 front = front + 1; 24 arr[front] = num; 25 rear ++; 26 } else if(isFull()) { 27 throw new RuntimeException("The queue is full and data cannot be added"); 28 } else { 29 rear = (rear + 1) % maxSize; 30 arr[rear] = num; 31 } 32 } 33 34 /** 35 * Get the header data from the queue 36 * @return 37 */ 38 public int get() { 39 if(isEmpty()) { 40 throw new RuntimeException("The queue is empty and the data cannot be retrieved"); 41 } else { 42 if (front == rear & front + 1 == maxSize) { 43 int value = arr[front]; 44 arr[front] = null; 45 front = rear = -1; 46 return value; 47 } else if (front == rear & front == 0) { 48 int value = arr[front]; 49 arr[front] = null; 50 front = rear = -1; 51 return value; 52 } else { 53 int value = arr[front]; 54 arr[front] = null; 55 front = (front + 1) % maxSize; 56 return value; 57 } 58 } 59 } 60 61 /** 62 * Determines if the queue is empty 63 * @return 64 */ 65 public Boolean isEmpty() { 66 return front == -1; 67 } 68 69 /** 70 * Determine if the queue is full 71 * @return 72 */ 73 public Boolean isFull() { 74 if (front == rear & rear == 0) { 75 return false; 76 } else if (rear + 1 == maxSize) { 77 if (front == 0) { 78 return true; 79 } 80 } else if (rear + 1 == front) { 81 return true; 82 } 83 return false; 84 } 85 86 public void show() { 87 if (isEmpty()) { 88 System.out.println("The queue is empty!"); 89 return; 90 } 91 if (rear == front | rear > front) { 92 for (int i = front; i < arr.length; i++) { 93 System.out.printf("arr[%d]:%d\n",i,arr[i]); 94 } 95 } else { 96 for (int i = front; i < arr.length; i++) { 97 System.out.printf("arr[%d]:%d\n",i,arr[i]); 98 } 99 for (int i = rear; i < front; i++) { 100 System.out.printf("arr[%d]:%d\n",i,arr[i]); 101 } 102 } 103 } 104 105 }
1 import java.util.Scanner; 2 3 /** 4 * @author Haidnor 5 * @creat 2020-03-12-16:05 6 */ 7 public class CircleQueueTest { 8 public static void main(String[] args) { 9 CircleQueue queue = new CircleQueue(5); 10 11 Scanner scanner = new Scanner(System.in); 12 Boolean loop = true; 13 while (loop) { 14 System.out.println("\ns(show): 查看队列"); 15 System.out.println("a(add): 加入数据"); 16 System.out.println("g(get): 取出数据"); 17 System.out.println("e(exit): 退出程序"); 18 System.out.print(">>> 请输入指令:"); 19 char command = scanner.next().charAt(0); 20 switch (command) { 21 case ‘s‘ : 22 queue.show(); 23 break; 24 case ‘a‘ : 25 System.out.print(">>>输入数字:"); 26 try { 27 int num = scanner.nextInt(); 28 queue.add(num); 29 } catch (Exception e) { 30 System.out.println(e.getMessage()); 31 } 32 break; 33 case ‘g‘ : 34 try { 35 int num = queue.get(); 36 System.out.println("取出的数字为:" + num); 37 } catch (Exception e) { 38 System.out.println(e.getMessage()); 39 } 40 break; 41 case ‘e‘ : 42 scanner.close(); 43 loop = false; 44 break; 45 default : 46 break; 47 } 48 } 49 System.out.println("已终止程序!"); 50 } 51 }
标签:next img 输入 circle rmi test The rom private
原文地址:https://www.cnblogs.com/Haidnor/p/12488663.html