java数组实现队列及环形队列的过程解析

发布时间:2021-09-16 17:49:03 作者:chen
来源:亿速云 阅读:126

本篇内容介绍了“java数组实现队列及环形队列的过程解析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

代码内容

ArrayQueue---用数组实现队列

package com.structure;import java.util.Scanner;/** * @auther::9527 * @Description: 数组模拟队列 * @program: jstl2 * @create: 2019-10-05 08:58 */public class ArrayQueueDemo {  public static void main(String[] args) {    Scanner scanner = new Scanner(System.in);    //测试    ArrayQueue queue = new ArrayQueue(3);    char key = ' '; //接受用户输入    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):查看队列头部的数据");      System.out.println("按提示输入......");      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:          System.out.println("...输入不合法,请重新输入...");          break;      }    }    System.out.println("程序退出....");  }}//使用数组模拟队列--编写一个ArrayQueueclass ArrayQueue {  private int maxSize; //数组的最大容量  private int front; //队列头  private int rear; //队列尾  private int[] arr; //存放数据的数组,模拟队列.  //创建队列的构造器  public ArrayQueue(int arrMaxSize) {    maxSize = arrMaxSize;    arr = new int[maxSize];    front = -1;    rear = -1;  }  //判断队列是否已经满了  public boolean isFull() {    return rear == maxSize - 1;  }  //判断队列是否为空  public boolean isEmpty() {    return rear == front;  }  //添加数据到队列  public void addQueue(int n) {    //判断队列是否已满    if (isFull()) {      System.out.println("队列已满,不能添加");      return;    }    rear++; //指针后移    arr[rear] = n;  }  //数据出队列  public int getQueue() {    //判断队列是否为空    if (isEmpty()) {      throw new RuntimeException("队列为空,不能取数据");    }    front++; //头部指针后移    return arr[front];  }  //显示队列的所有数据  public void showQueue() {    if (isEmpty()) {      System.out.println("队列为空,没有数据");      return;    }    for (int i = 0; i < arr.length; i++) {      System.out.printf("arr[%d]=%d\n", i, arr[i]);    }  }  //显示队列的头部数据,这个不是取出数据  public int headQueue() {    //判断是否为空    if (isEmpty()) {      System.out.println("队列为空,没有数据....");      throw new RuntimeException("队列为空,没有数据....");    }    return arr[front + 1];  }}

改进版---环形队列:

环形队列代码

package com.structure;import java.util.Scanner;/** * @auther::9527 * @Description: 环形队列 * @program: jstl2 * @create: 2019-10-05 09:53 */public class CircleArrayQueueDemo {  public static void main(String[] args) {    //创建一个环形队列 这里输入最大数是4,其实有效的是3    CircleArray queue = new CircleArray(4);    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) {            // TODO: handle exception            System.out.println(e.getMessage());          }          break;        case 'h': // 查看队列头的数据          try {            int res = queue.headQueue();            System.out.printf("队列头的数据是%d\n", res);          } catch (Exception e) {            // TODO: handle exception            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]=arr[0]  private int front;  //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 temp = arr[front];    front = (front + 1) % maxSize;    return temp;  }  //显示队列的所有数据  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;  }  //显示队列的头部数据  public int headQueue(){    //判断是否为空    if (isEmpty()){      throw new RuntimeException("队列为空,没有数据");    }    return arr[front];  }}

“java数组实现队列及环形队列的过程解析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 05-环形队列
  2. golang使用数组模拟环形队列(demo)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:ASP.NET5和MVC6中Configuration配置信息管理的示例分析

下一篇:如何制作网页WEB打印控件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》