队列的模拟及环形队列思路

定义

  • 队列是一个 有序列表,可以用 数组或是 链表来实现。
  • 遵循 先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出

模拟思路

  • 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量
  • 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front及 rear分别记录队列前后端的下标,front 会随着数据输出而改变,而 rear则是随着数据输入而改变,如图所示

队列的模拟及环形队列思路

入队出队操作模拟

当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:

  • 将尾指针往后移:rear+1 , 当 front == rear 时,队列为空
  • 若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear所指的数组元素中,否则无法存入数据。 rear == maxSize – 1时,队列满

注意:front指向的是队列首元素的 前一个位置

实现代码

java;collapse:true;;gutter:true; import java.util.Scanner;</p> <p>public class ArrayQueueDemo { public static void main(String[] args) { ArrayQueue queue = new ArrayQueue(3); Scanner scanner = new Scanner(System.in); boolean flag = true; while (flag){ System.out.println("输入a(add)添加数据"); System.out.println("输入g(get)取出数据"); System.out.println("输入s(show)显示所有数据"); System.out.println("输入h(head)显示头部数据"); System.out.println("输入e(exit)退出程序"); char c = scanner.next().charAt(0); switch (c){ case 'a': System.out.println("请输入数据"); int num = scanner.nextInt(); queue.addNum(num); break; case 'g': try { queue.getQueue(); System.out.println("取出成功"); }catch (Exception e){ System.out.println(e.getMessage()); } break; case 's': queue.showQueue(); break; case 'h': try { queue.headQueue(); }catch (Exception e){ System.out.println(e.getMessage()); } break; case 'e': flag = false; System.out.println("程序已退出"); break; default: break; } } scanner.close(); } }</p> <p>class ArrayQueue{ private int maxSize;//队列的大小 private int front;//指向队列首元素的前一个位置 private int rear;//指向队列的尾元素 private int[] arr;//用数组来实现队列</p> <pre><code>//构造方法初始化 public ArrayQueue(int maxSize) { this.maxSize = maxSize; front = -1;//指向的是队列第一个元素的前一个位置 rear = -1; arr = new int[maxSize]; } /** * 判断队列是否装满 * @return */ public boolean isFull(){ return rear == maxSize-1; } /** * 判断队列是否为空 * @return */ public boolean isEmpty(){ return front == rear; } /** * 为队列添加数据 * @param num */ public void addNum(int num){ if (isFull()){ System.out.println("队列已经满了,无法再加入数据"); return; } rear++; arr[rear] = num; } /** * 取出队列中的数据 * @return */ public int getQueue(){ if (isEmpty()){ throw new RuntimeException("队列为空,不能取出数据"); } front++; return arr[front]=0; } /** * 显示队列的所有数据 */ public void showQueue(){ if (isEmpty()) { System.out.println("队列为空,没有数据"); return; } for (int i = 0; i </code></pre> <pre><code> 运行结果 ![队列的模拟及环形队列思路](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/2980763-20220916140908967-616811875.png) ![队列的模拟及环形队列思路](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/2980763-20220916140936187-1079187307.png) **注意**:因先入先出的原则,front和rear最终都会在队列顶部,所以上述队列只能一次性使用,没有达到复用的效果,因此我们要用到环形队列 ### 环形队列 思路: * front变量含义调整:front变量指向 **队首元素**,初值为0 * rear变量含义调整:rear变量指向队尾元素的 **下一个元素**,初值为0。规定空出一个位置 * 队列为空的判定条件:front == rear * 队列为满的判定条件:(rear + 1) % maxSize == front * 队列中有效元素的个数:(rear - front + maxSize) % maxSize * 入队和出队时,都需要让标记 **对maxSize取模** * ;collapse:true;;gutter:true;
import java.util.Scanner;

public class CyclicArrayQueueDemo {
public static void main(String[] args) {
CyclicArrayQueue queue = new CyclicArrayQueue(3);
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag){
System.out.println("输入a(add)添加数据");
System.out.println("输入g(get)取出数据");
System.out.println("输入s(show)显示所有数据");
System.out.println("输入h(head)显示头部数据");
System.out.println("输入e(exit)退出程序");
char c = scanner.next().charAt(0);
switch (c){
case ‘a’:
System.out.println("请输入数据");
int num = scanner.nextInt();
queue.addNum(num);
break;
case ‘g’:
try {
int n = queue.getQueue();
System.out.println("取出的数是:"+n);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case ‘s’:
queue.showQueue();
break;
case ‘h’:
try {
queue.headQueue();
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case ‘e’:
flag = false;
System.out.println("程序已退出");
break;
default:
break;
}
}
scanner.close();
}
}

class CyclicArrayQueue {
private int maxSize;//队列的大小
private int front;//front变量指向队首元素,初值为0
private int rear;//rear变量指向队尾元素的下一个元素,初值为0。规定空出一个位置
private int[] arr;//用数组来实现队列

public CyclicArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}

/**
* 判断队列是否装满
*
* @return
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}

/**
* 判断队列是否为空
*
* @return
*/
public boolean isEmpty() {
return front == rear;
}

/**
* 为队列添加数据
*
* @param num
*/
public void addNum(int num) {
if (isFull()) {
System.out.println("队列已经满了,无法再加入数据");
return;
}
arr[rear] = num;
rear = (rear + 1) % maxSize;
}

/**
* 取出队列中的数据
*
* @return
*/
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能取出数据");
}
int value = arr[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("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}

public int size() {
return (rear + maxSize – front) % maxSize;
}

/**
* 显示队列的头数据,注意不是取出数据
* @return
*/
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}
return arr[front];
}
}

Original: https://www.cnblogs.com/wyh518/p/16725753.html
Author: 腹白
Title: 队列的模拟及环形队列思路

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/604839/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球