首页 > 编程知识 正文

环形队列和循环队列,实现环形队列各种基本运算的算法

时间:2023-05-03 11:11:00 阅读:191065 作者:2029

目录

前言

一、队列

二、使用步骤

注意初始的定义:

1.创建一个CircleArray

2.进行测试

总结


前言

利用数组,通过取模的方式来实现环形队列

一、队列

1) 队列是一个有序列表,可以用数组或是链表来实现。

2) 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出

 

二、使用步骤 注意初始的定义:  front变量的含义:front指向队列的第一个元素,front的初始值为0。 rear变量的含义:rear指向队列的最后一个元素的后一个位置,rear的初始值为0。约定:当rear指向的位置为空时,且下一个是front所指向的位置,此时队列为满。队列满时,条件是:(rear +1)%maxSize = front。对队列为空的条件:rear == front。.队列中有效的数据的个数:(rear + maxSize-front)% maxSize 。
 1.创建一个CircleArray class CircleArray {private int maxSize;// 数组最大容量private int front;// 队列头部,指向第一个元素,初始值为0private int rear;// 队列尾部,指向最后一个元素的下一位(即空值),初始值为0// 留一个空元素缓冲,即rear指向的元素为空值,且当此空值下一个元素是front所指的元素时,此时定义为队列“已满”private int[] arr;// 定义数组,模拟环形队列public CircleArray(int maxSize) {super();this.maxSize = maxSize;arr = new int[maxSize];// 初始化数组长度}// 判断队列是否已满public boolean isFull() {return (rear + 1) % maxSize == front;// 考虑取模}// 判断队列是否为空public boolean isEmpty() {return rear == front;}// 添加数据到队伍,rear后移一位(取模)public void addValue(int n) {if (isFull()) {System.out.println("队列已满,无法添加数据");} else {arr[rear] = n;// 这里rear指向的数为空,因此直接将数值添加到arr[rear]rear = (rear + 1) % maxSize;// 将rear后移一位时,这里必须考虑取模}}// 获取队列的数据,front后移一位(取模)public int getValue() {if (isEmpty()) {throw new RuntimeException("队列为空,无法获取数据");}int value = arr[front];front = (front + 1) % maxSize;return value;}// 显示队列的所有数据public void showValue() {if (isEmpty()) {System.out.println("队列为空,没有数据");} else {for (int i = front; i < front + size(); i++) {System.out.println("arr["+i%maxSize+"]=" + arr[i % maxSize]);// 注意取模}}}// 求出当前队列有效数据的个数public int size() {return (rear + maxSize - front) % maxSize;// 分母加上maxSize,可以避免负数出现}// 显示队列的头数据, 注意不是取出数据public int headValue() {if (isEmpty()) {throw new RuntimeException("队列为空,没有数据");}return arr[front];}} 2.进行测试 import java.util.Scanner;public class CircleArrayQueue {public static void main(String[] args) {System.out.println("测试数组模拟环形队列的案例");// 创建一个环形队列CircleArray queue = new CircleArray(4); // 说明设置 4, 其队列的有效数据最大是 3char 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':queue.showValue();break;case 'a':System.out.println("请输入一个数");int value = scanner.nextInt();queue.addValue(value);break;case 'g': // 取出数据try {int res = queue.getValue();System.out.println("取出的数据是:"+res);} catch (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}break;case 'h': // 查看队列头的数据try {int res = queue.headValue();System.out.println("队列头的数据是:"+res);} catch (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}break;case 'e': // 退出loop = false;break;default:break;}}System.out.println("程序已退出");scanner.close();}} 总结

代码如上,按照代码可以设定环形队列

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。