数组模拟环形队列案例和思路
2019-08-09 11:47
369 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40823802/article/details/98945376
思路如下:
**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 。
代码如下:代码内有详细说明
public class CircleArrayQueueDemo { public static void main(String[] args) { System.out.println("数组模拟环形队列案例!!"); //创建一个环形队列 CircleArray queue = new CircleArray(4);//设置为4,队列中最大有效数据是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': queue.showQueue(); break; case 'a': System.out.println("输入一个数"); int value = scanner.nextInt(); queue.addQueue(value); break; case 'g': try { int res = queue.getQueue(); System.out.printf("取出的数据是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = queue.headQueue(); System.out.printf("队列头的元素是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; default: break; } } System.out.println("程序退出"); } } class CircleArray { private int maxSize;//表示数组的最大容量。 //front 变量的含义做一个调整:front 就指向队列的第一元素,也就是说 arr[front] //front 的初始值=0 private int front;//队列头 //rear 变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置。因为希望空出 //一个空间做为约定 rear 的初始值=0 private int rear;//队列尾 private int[] arr;//该数组用于存放数据,模拟队列。 public CircleArray(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; } //判断队列是否满 public boolean isFull() { return (rear + 1) % maxSize == front; } //判断队列是否为空 public boolean isEmpty() { return rear == front; } //添加数据到队列 public void addQueue(int n) { //判断队列是否满 if (isFull()) { System.out.println("队列满,不能加入数据~~"); return; } //直接将数据加入 arr[rear] = n; //将rear后移 这里必须考虑取模 rear = (rear + 1) % maxSize; } //获取队列的数据,出队列 public int getQueue() { //判断队列是否空 if (isEmpty()) { //通过抛出异常 throw new RuntimeException("队列空,不能取数据"); } //front是指向队列的第一个元素 //1.先把front对应的值保留到一个临时变量 //2.将front后移 //3.将临时保存的变量返回 int value = arr[front]; front = (front + 1) % maxSize; return value; } //显示队列的所有数据 public void showQueue() { //遍历 if (isEmpty()) { System.out.println("队列空的,没有数据!!!"); return; } //思路:从front开始遍历,遍历多少个元素 // 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; } //显示队列的头数据,注意不是取出数据 public int headQueue() { //判断 if (isEmpty()) { throw new RuntimeException("队列空的没有数据~~~"); } return arr[front]; } }
运行结果:
相关文章推荐
- 队列的底层实现(数组和环形数组)
- 环形队列的数组实现
- wait, notify, notifyAll, 简单数组模拟队列实现读者写者问题。
- 数据结构循环队列——数组模拟实现
- 队列(数组模拟)
- c++模拟队列编程案例(参考c++书籍)
- 【模拟】【环形数组】-UVA-133- The Dole Queue
- 用固定大小的数组模拟栈和队列
- HDU5929 Basic Data Structure,ccpc,模拟双端队列(两倍大的数组从中间开始向两旁拓展)
- c语言数据结构实现-数组队列/环形队列
- 反复把元素移到队头或队尾(数组模拟队列的方法)
- Java 数组模拟队列
- 数据结构栈和队列的数组实现和链表实现的4个头文件(完全个人思路)
- 数据结构队列之环形队列的动态数组实现:queue
- 数据结构(Java 队列模拟)本代码重在学习数据结构思路,代码完整性欠缺,请见谅
- joj 2431: Shift and Increment (模板队列与数组模拟队列的对比练习)
- 无锁队列的环形数组实现
- 数据结构03.1 队列模拟(数组)
- 2017浙工大院赛预赛 D 简单的数据结构【双端队列||数组模拟】
- 1443. Printer Queue(用一个数组模拟队列,优先级高的先出队列)