04队列实现思路
队列特点:先进先出
队列可以使用数组或链表来实现
数组模拟队列
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下,其中maxSize是该队列的最大容量。
因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front及rear分别记录队列前后端的下标,front会随着数据输出而改变,rear会随着数据的输入而改变。
使用数组实现队列
front 默认-1,队首
rear 默认-1,队尾
maxSize 队列的最大容量
加数据,在队尾加,rear+1
取数据,在队首取,front+1
添加到队列,分析
1,将rear+1,尾指针后移(当rear==front时,队列为空,可存入;非空时,当rear=maxSize-1时,队列已满,无法存入)
代码实现:
1 package com.datastack.datastack.queue; 2 3 import java.util.Scanner; 4 5 /* 6 * 队列(数组实现) 7 */ 8 public class ArrQueue { 9 private int maxSize;//队列最大值 10 private int front;//队首,指向队列首的前一个位置 11 private int rear;//队尾,指向队列尾的序号 12 private int[] arr;//存放队列数据的数组 13 14 /** 15 * 创建队列 16 * @param maxSize 17 */ 18 public ArrQueue(int maxSize){ 19 this.maxSize = maxSize; 20 this.arr = new int[maxSize]; 21 this.front = -1; 22 this.rear = -1; 23 } 24 25 /** 26 * 判断队列是否已满 27 * @return 28 */ 29 public boolean isFull(){ 30 return rear == maxSize - 1; 31 } 32 33 /** 34 * 判断队列是否为空 35 * @param args 36 */ 37 public boolean isEmpty(){ 38 return rear == front; 39 } 40 41 /** 42 * 添加数据到队列 43 * @param args 44 */ 45 public void addQueue(int n){ 46 //判断队列是否满 47 if(isFull()){ 48 System.out.println("队列已满,不能加入数据。"); 49 return; 50 } 51 rear++; 52 arr[rear] = n; 53 } 54 55 /** 56 * 出队列 57 * @param args 58 */ 59 public int getQueue(){ 60 //判断队列是否为空 61 if(isEmpty()){ 62 //通过抛出异常 63 throw new RuntimeException("队列空,不能取数据"); 64 } 65 front++; 66 return arr[front]; 67 } 68 69 /** 70 * 显示队列数据 71 * @param args 72 */ 73 public void showQueque(){ 74 if(isEmpty()){ 75 System.out.println("队列为空。"); 76 return; 77 } 78 for(int i=0;i<arr.length;i++){ 79 System.out.printf("arr[%d]=%d\t",i,arr[i]); 80 } 81 } 82 83 /** 84 * 显示队头 85 * @param args 86 */ 87 public int headQueue(){ 88 if(isEmpty()){ 89 throw new RuntimeException("队列为空。"); 90 } 91 return this.arr[front+1]; 92 } 93 94 public static void main(String[] args) { 95 //创建一个队列 96 ArrQueue arrQueue = new ArrQueue(3); 97 char key = ' ';//接收用户输入 98 Scanner scanner = new Scanner(System.in); 99 boolean loop = true; 100 while(loop){ 101 System.out.println("s(show):显示队列"); 102 System.out.println("e(exit):退出程序"); 103 System.out.println("a(add):添加数据到队列"); 104 System.out.println("g(get):从队列取出数据"); 105 System.out.println("h(head):查看队列头的数据"); 106 key = scanner.next().charAt(0); 107 switch (key) { 108 case 's'://显示队列值 109 arrQueue.showQueque(); 110 break; 111 case 'a'://入队 112 System.out.println("请输入一个数"); 113 int value = scanner.nextInt(); 114 arrQueue.addQueue(value); 115 break; 116 case 'g'://出队 117 try { 118 int res = arrQueue.getQueue(); 119 System.out.println(res); 120 } catch (Exception e) { 121 System.out.println(e.getMessage()); 122 } 123 break; 124 case 'h'://打印对首 125 try { 126 int res = arrQueue.headQueue(); 127 System.out.println(res); 128 } catch (Exception e) { 129 System.out.println(e.getMessage()); 130 } 131 break; 132 case 'e'://退出程序 133 scanner.close(); 134 loop = false; 135 break; 136 137 default: 138 break; 139 } 140 } 141 System.out.println("程序退出"); 142 143 } 144 }
package com.datastack.datastack.queue; import java.util.Scanner; /* * 队列(数组实现) */ public class ArrQueue { private int maxSize;//队列最大值 private int front;//队首,指向队列首的前一个位置 private int rear;//队尾,指向队列尾的序号 private int[] arr;//存放队列数据的数组 /** * 创建队列 * @param maxSize */ public ArrQueue(int maxSize){ this.maxSize = maxSize; this.arr = new int[maxSize]; this.front = -1; this.rear = -1; } /** * 判断队列是否已满 * @return */ public boolean isFull(){ return rear == maxSize - 1; } /** * 判断队列是否为空 * @param args */ public boolean isEmpty(){ return rear == front; } /** * 添加数据到队列 * @param args */ public void addQueue(int n){ //判断队列是否满 if(isFull()){ System.out.println("队列已满,不能加入数据。"); return; } rear++; arr[rear] = n; } /** * 出队列 * @param args */ public int getQueue(){ //判断队列是否为空 if(isEmpty()){ //通过抛出异常 throw new RuntimeException("队列空,不能取数据"); } front++; return arr[front]; } /** * 显示队列数据 * @param args */ public void showQueque(){ if(isEmpty()){ System.out.println("队列为空。"); return; } for(int i=0;i<arr.length;i++){ System.out.printf("arr[%d]=%d\t",i,arr[i]); } } /** * 显示队头 * @param args */ public int headQueue(){ if(isEmpty()){ throw new RuntimeException("队列为空。"); } return this.arr[front+1]; } public static void main(String[] args) { //创建一个队列 ArrQueue arrQueue = new ArrQueue(3); char key = ' ';//接收用户输入 Scanner scanner = new Scanner(System.in); boolean loop = true; while(loop){ System.out.println("s(show):显示队列"); System.out.println("e(exit):退出程序"); System.out.println("a(add):添加数据到队列"); System.out.println("g(get):从队列取出数据"); System.out.println("h(head):查看队列头的数据"); key = scanner.next().charAt(0); switch (key) { case 's'://显示队列值 arrQueue.showQueque(); break; case 'a'://入队 System.out.println("请输入一个数"); int value = scanner.nextInt(); arrQueue.addQueue(value); break; case 'g'://出队 try { int res = arrQueue.getQueue(); System.out.println(res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h'://打印对首 try { int res = arrQueue.headQueue(); System.out.println(res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e'://退出程序 scanner.close(); loop = false; break; default: break; } } System.out.println("程序退出"); } } 问题分析
1,目前数组只能使用一次,不能复用
2,需要改进成环形队列