2_数据结构与算法
1、队列
1-1. 运用场景
银行排队案例
餐厅排队案例等…
1-2. 队列介绍
- 队列是一个有序列表,可以用数组或是链表来实现。
- 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
1-3. 数组模拟队列
1、思路
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量
因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front 及 rear 分别记录队列前后端的下标, front 会随着数据输出而改变,而 rear 则是随着数据输入而改变
当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:思路分析
将尾指针往后移:rear+1 , 当 front == rear 【空】
若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear 所指的数组元素中,否则无法存入数据。rear== maxSize - 1[队列满]
2、代码
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop){
System.out.println("s(show):显示队列!");
System.out.println("a(add):添加队列!");
System.out.println("g(get):获取队列!");
System.out.println("h(head):显示队列头部!");
System.out.println("e(exit):退出程序!");
key = scanner.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数");
int value = scanner.nextInt();
arrayQueue.addArrayQueue(value);
break;
case 'g':
try {
int val = arrayQueue.getArrayQueue();
System.out.println("取出的数据是:"+val);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
int head = 0;
try {
head = arrayQueue.headQueue();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("头部数据是:"+head);
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~");
}
}
class ArrayQueue {
private int maxSize;//队列最大容量
private int front;//头指针
private int rear;//尾指针
private int[] array;//存放数组数据,模拟队列
/**
* 创建构造器
*
* @param arrayMaxSize
*/
public ArrayQueue(int arrayMaxSize) {
this.maxSize = arrayMaxSize;
array = new int[maxSize];
front = -1;//指向队列头部的前一个位置
rear = -1;//指向队列尾部
}
/**
* 判断队列是否满
*/
public boolean isFull() {
return rear == maxSize - 1;
}
/**
* 判断队列是否为空
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加队列
*/
public void addArrayQueue(int n) {
if (isFull()) {
System.out.println("队列已满,请勿继续插入~");
return;
}
rear++;
array[rear] = n;
}
/**
* 从队列取数据
*/
public int getArrayQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,无法取出数据~");
}
front++;
return array[front];
}
/**
* 显示所有队列数据
*/
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空~");
return;
}
for (int i = 0; i < array.length; i++) {
System.out.printf("array[%d]=%d\n",i,array[i]);
}
}
/**
* 显示数头部,非取
*/
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,无数据~");
}
return array[front+1];
}
}
1-4. 数组模拟环形队列
可参考:https://blog.csdn.net/qq_41473905/article/details/103480136
-
注意:
- 此处的rear是数组数据的下一个指针位置,并非数据的位置
- 最大有效数据为maxSize-1
-
思路:
-
尾索引的下一个为头索引时表示队列满,即将队列容量空出一个作为约定,这个在做判断队列满的时候需要注意 (rear + 1) % maxSize == front [满]
-
rear == front [空]
-
1. Front变量的含义做一个调整:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front初始值=0.
2. Rear变量的含义调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间做约定,rear初始值=0.
3. 当队列满时,条件是(rear + 1)%maxSize == front【满】
4. 队列为空的条件,rear==front【空】
5. 当这样分析后,队列中有效的数据的个数(rear + maxSize – front)%maxSize
6. 我们就可以在原来的队列上得到一个环形队列。
-
-
代码:
public class CircleArrayQueueDemo { public static void main(String[] args) { System.out.println("测试数组模拟环形队列案例~"); CircleArray circleArray = new CircleArray(4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while (loop) { System.out.println("s(show):显示队列!"); System.out.println("a(add):添加队列!"); System.out.println("g(get):获取队列!"); System.out.println("h(head):显示队列头部!"); System.out.println("e(exit):退出程序!"); key = scanner.next().charAt(0); switch (key) { case 's': circleArray.showQueue(); break; case 'a': System.out.println("请输入一个数"); int value = scanner.nextInt(); circleArray.addArrayQueue(value); break; case 'g': try { int val = circleArray.getArrayQueue(); System.out.println("取出的数据是:" + val); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': int head = 0; try { head = circleArray.headQueue(); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println("头部数据是:" + head); break; case 'e': scanner.close(); loop = false; break; default: break; } } System.out.println("程序退出~"); } } class CircleArray { private int maxSize;//队列最大容量 private int front;//头指针 private int rear;//尾指针 private int[] array;//存放数组数据,模拟队列 public CircleArray(int arrayMaxSize) { this.maxSize = arrayMaxSize; array = new int[maxSize]; } /** * 判断队列是否满 */ public boolean isFull() { return (rear + 1) % maxSize == front; } /** * 判断队列是否为空 */ public boolean isEmpty() { return rear == front; } /** * 添加队列 */ public void addArrayQueue(int n) { if (isFull()) { System.out.println("队列已满,请勿继续插入~"); return; } array[rear] = n; rear = (rear + 1) % maxSize; } /** * 从队列取数据 */ public int getArrayQueue() { if (isEmpty()) { throw new RuntimeException("队列为空,无法取出数据~"); } int value = array[front]; front = (front + 1) % maxSize; return value; } /** * 显示所有队列数据 */ public void showQueue() { if (isEmpty()) { System.out.println("队列为空~"); return; } for (int i = front; i < front + size(); i++) { System.out.printf("array[%d]=%d\n", i % maxSize, array[i % maxSize]); } } /** * 显示数头部,非取 */ public int headQueue() { if (isEmpty()) { throw new RuntimeException("队列为空,无数据~"); } return array[front]; } /** * 求出当前队列有效数据 */ public int size() { return (rear + maxSize - front) % maxSize; } }