特点:先入先出(类似于银行排队问题)


一、数组模拟队列

package com.company;import java.util.Scanner;/*** @author:抱着鱼睡觉的喵喵* @date:2021/1/31* @description: the array simulate the queue*/
public class ArrayQueue {//test array simulate queuepublic static void main(String[] args) {ArraySimulateQueue arraySimulateQueue = new ArraySimulateQueue(3);char key=' ';Scanner scanner = new Scanner(System.in);boolean flag=true;while (flag){System.out.println("s:show queue");System.out.println("a:add data to the queue");System.out.println("g:the data obtained from the queue");System.out.println("h:view queue header data");System.out.println("e:exit the program");key=scanner.next().charAt(0);switch (key){case 's':arraySimulateQueue.showQueue();break;case 'a':System.out.println("please entry a data!");int data=scanner.nextInt();arraySimulateQueue.enQueue(data);break;case 'g':try {int result=arraySimulateQueue.deQueue();System.out.println("the data obtained from the queue is:"+result);}catch (Exception e){System.out.println(e.getMessage());}break;case 'h':try {int frontData=arraySimulateQueue.getFront();System.out.println("the head of the queue data is:"+frontData);}catch (Exception e){System.out.println(e.getMessage());}break;case 'e':scanner.close();flag=false;return;default:System.out.println("the characters you entered are wrong!");}}System.out.println("program has exited!");}
}
/*** use array to simulate queue*/
class ArraySimulateQueue{private int maxSize;private int front;private int rear;private int[] arr;public ArraySimulateQueue(int currentMaxSize){this.maxSize=currentMaxSize;this.front=-1;this.rear=-1;arr=new int[maxSize];}//determine whether the queue is fullpublic boolean isFull(){return rear == maxSize-1;}//determine whether the queue is emptypublic boolean isEmpty(){return rear == front;}//enqueuepublic void enQueue(int data){if (isFull()){System.out.println("the queue is full and cannot add data!");return;}else{this.rear++;arr[rear]=data;System.out.println("successful data entry!");}}//dequeuepublic int deQueue(){if (isEmpty()){throw new RuntimeException("the queue is empty and cannot get data!");}else{this.front++;int result=arr[front];arr[front]=0;return result;}}//show datapublic void showQueue(){for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d]=%d",i,arr[i]);;System.out.println();}}public int getFront(){if (isEmpty()){throw new RuntimeException("the queue is empty and cannot get header data!");}else{return arr[front+1];}}
}

分析缺点:

由图可以看出使用过的内存单元将不能够继续使用,所以使用环形队列可以解决内存资源浪费的问题

环形队列

实现方式1

package com.company;import java.util.Scanner;/*** @author:抱着鱼睡觉的喵喵* @date:2021/2/1* @description: the array simulate the circular queue*/
public class CircleArrayQueue {public static void main(String[] args) {CircleQueue circleQueue = new CircleQueue(3);boolean flag = true;Scanner scanner = new Scanner(System.in);char key = ' ';while (flag) {System.out.println("s:show queue");System.out.println("a:add data to the queue");System.out.println("g:get data from the queue");System.out.println("h:view queue header data");System.out.println("e:exit the program");key = scanner.next().charAt(0);switch (key) {case 's':circleQueue.showData();break;case 'a':System.out.println("please enter a data:");int result = scanner.nextInt();circleQueue.enQueue(result);break;case 'g':try {System.out.printf("the data obtained from queue is:%d", circleQueue.dequeue());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {System.out.printf("header data is:%d", circleQueue.getFront());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'e':scanner.close();flag = false;}}}
}/*** Circular simulate queue*/
class CircleQueue {private int maxSize;private int front;private int rear;private int[] arr;private int num;public CircleQueue(int currentMaxSize) {this.maxSize = currentMaxSize;this.front = -1;this.rear = -1;arr = new int[maxSize];this.num = 0;           //record the number of array units and used to determine whether the circular queue is full or empty}//determine  whether the circular queue is fullpublic boolean isFull() {return num == maxSize;}//determine whether the circular queue is emptypublic boolean isEmpty() {return num == 0;}//enqueuepublic void enQueue(int data) {if (isFull()) {System.out.println("the circular is full,unable to add data!");} else if ((rear + 1 == maxSize) && (!isFull())) {rear = 0;arr[rear] = data;num++;} else {rear++;arr[rear] = data;num++;}}//dequeuepublic int dequeue() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty,unable to leave the queue!");} else if ((front + 1 == maxSize) && (!isEmpty())) {front = 0;int result = arr[front];arr[front] = 0;num--;return result;} else {front++;int result = arr[front];arr[front] = 0;num--;return result;}}//get the circular queue header datapublic int getFront() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty cannot get header data!");} else if (front +1 == maxSize){return arr[0];} else {return arr[front+1];}}//show datapublic void showData() {for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d]=%d", i, arr[i]);System.out.println();}}
}

实现方式2

package com.company;import java.util.Scanner;/*** @author:抱着鱼睡觉的喵喵* @date:2021/2/2* @description:*/
public class CircleArrayQueue2 {public static void main(String[] args) {CircleQueue2 circleQueue = new CircleQueue2(3);boolean flag = true;Scanner scanner = new Scanner(System.in);char key = ' ';while (flag) {System.out.println("s:show queue");System.out.println("a:add data to the queue");System.out.println("g:get data from the queue");System.out.println("h:view queue header data");System.out.println("e:exit the program");key = scanner.next().charAt(0);switch (key) {case 's':circleQueue.showData();break;case 'a':System.out.println("please enter a data:");int result = scanner.nextInt();circleQueue.enQueue(result);break;case 'g':try {System.out.printf("the data obtained from queue is:%d", circleQueue.dequeue());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {System.out.printf("header data is:%d", circleQueue.getFront());System.out.println();} catch (Exception e) {System.out.println(e.getMessage());}break;case 'e':scanner.close();flag = false;}}}
}/*** Circular simulate queue*/
class CircleQueue2 {private int maxSize;private int front;private int rear;private int[] arr;public CircleQueue2(int currentMaxSize) {this.maxSize = currentMaxSize;this.front = 0;this.rear = 0;arr = new int[maxSize];}//determine  whether the circular queue is fullpublic boolean isFull() {return (rear + 1) % maxSize == front;}//determine whether the circular queue is emptypublic boolean isEmpty() {return rear == front;}//enqueuepublic void enQueue(int data) {if (isFull()) {System.out.println("the circular is full,unable to add data!");} else {arr[rear] = data;rear = (rear + 1) % maxSize;}}//dequeuepublic int dequeue() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty,unable to leave the queue!");} else {int result = arr[front];arr[front] = 0;front = (front + 1) % maxSize;return result;}}//get the circular queue header datapublic int getFront() {if (isEmpty()) {throw new RuntimeException("the circular queue is empty cannot get header data!");} else {return arr[front % maxSize];}}//show datapublic void showData() {for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d]=%d", i, arr[i]);System.out.println();}}
}

数据结构和算法之数组模拟队列相关推荐

  1. (数据结构与算法)数组模拟队列和环形队列

    文章目录 数组模拟队列 思路 代码实现 问题分析并优化 数组模拟环形队列 思路 代码实现 数组模拟队列 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则.即:先存入队列的数据,要先取 ...

  2. golang数据结构与算法——稀疏数组、队列和链表

    文章目录 一 稀疏数组 1.1 先看一个实际的需求 1.2 稀疏数组基本介绍 1.3 稀疏数组举例说明 1.4 把数组转换为稀疏数组实现 1.5 把稀疏数组还原为原数组 二 队列 2.1 队列的介绍 ...

  3. 菜鸟窝-数据结构与算法之数组实现队列

    categories: 数据结构与算法 tags: 队列 title: 数据结构与算法之队列 date: 数据结构之队列 定义:有序列表,可以通过数组或者链表实现,遵循先入先出的原则 数组实现普通队列 ...

  4. 20200120 数据结构和算法之 数组循环队列的实现

    数组循环队列针对数据量不大的情况下使用,可以快速地实现元素的入队和出队.入队和出队遵循先进先出(FIFO)的原则.结构体组成如下: typedef int datatype; typedef stru ...

  5. 不可上位!数据结构队列,老实排队,Java实现数组模拟队列及可复用环形队列

    文章目录 队列简介 数组模拟队列(无法复用) 数组模拟环形队列(可复用) 队列简介 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则.即先存入队列的数据,先取出,后存入的后取出. 示 ...

  6. 【算法】java 用 数组 模拟 队列

    本文为博主九师兄(QQ:541711153 欢迎来探讨技术)原创文章,未经允许博主不允许转载. 文章目录 1.概述 1.概述 本次使用java 用 数组 模拟 队列 package com.algor ...

  7. 数据结构与算法(2)——栈和队列

    前言:题图无关,只是好看,接下来就来复习一下栈和队列的相关知识 前序文章: 数据结构与算法(1)--数组与链表(https://www.jianshu.com/p/7b93b3570875) 栈 什么 ...

  8. 数组模拟队列(代码实现)

    数据结构可分为两种,第一种是线性结构,第二种是非线性结构,线性结构又分为连续存储和链表存储. 常见的线性结构有数组,链表,队列,栈: 以下是数组模拟队列的实现(队列特点就是先进先出): //数组模拟队 ...

  9. 数据结构与算法(C++)– 队列(Queue)

    数据结构与算法(C++)– 队列(Queue) 1.队列 先进先出(First in, First out) enqueue 入队,dequeue 出队 front 队头,rear / back 队尾 ...

最新文章

  1. 跟我学Springboot开发后端管理系统2:Mybatis-Plus实战
  2. pandas用read_csv时编码问题解决
  3. Ubuntu12.04 LTS 忘记登录密码的解决方法
  4. 容器技术之kubectl常用命令
  5. libevent evhttp学习——http客户端
  6. 计算机用户文件夹怎么改名称,win10修改用户名文件夹方法_win10怎么改用户文件夹名称-win7之家...
  7. k8s核心技术-Helm(chart模板的使用上)---K8S_Google工作笔记0048
  8. matlab 识别调试,有关matlab的人脸识别程序,但调试是不成功
  9. GNU make manual 翻译(四十六)
  10. 大数据分析-第八章 推荐系统
  11. vector的几种初始化及赋值方式
  12. access查找出生日期年份_Access时间日期比较查询的方法总结
  13. 抽象代数笔记2——群
  14. Redis【有与无】【Lettuce】L4.Redis Sentinel
  15. 【Flutter 问题系列第 15 篇】如何给 Flutter 中的图片设置透明度
  16. java双音频文件分频_分频电路作用,怎么来理解二分频电路?
  17. pink老师携程网制作(html+css)
  18. android 2048 动画,大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画
  19. 华硕启动修复无法自动修复此计算机怎么办,华硕电脑无法启动,自动也修复不了怎么办...
  20. AirPlay Android接收端学习一 协议

热门文章

  1. C# 十六进制字符串与数值类型之间转换
  2. linux操作系统版本 3100,Linux操作系统默认打开文件数
  3. dell r740如何做raid_戴尔入门级4K、IPS广色域显示器:S2721QS表现如何?
  4. tensorflow 数据格式
  5. java高效遍历匹配,使用cypher或遍历api仅匹配路径极端的单个节点
  6. 计算机word考试中的图文混排,2014招警考试公共基础计算机知识:Word的图文混排功能...
  7. java如何给数组倒置_数组元素倒置-Java
  8. centos6 安装bbr_Centos 升级内核安装 BBR 简易操作!
  9. python pandas dataframe 转json_python-将嵌套的json转换为pandas dataframe
  10. python for loop循环程序语句_python-带for循环的格式化输出_for-loop_酷徒编程知识库...