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

 更新时间:2019年10月09日 09:40:29   作者:小龙_T无限  
这篇文章主要介绍了java数组实现队列及环形队列实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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("程序退出....");
  }
}

//使用数组模拟队列--编写一个ArrayQueue
class 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+Struts2获取服务器地址的方法

    JAVA+Struts2获取服务器地址的方法

    这篇文章主要介绍了JAVA+Struts2获取服务器地址的方法,是Struts2的一个简单应用,具有一定的借鉴与参考价值,需要的朋友可以参考下
    2014-11-11
  • Spring MVC学习笔记之json格式的输入和输出

    Spring MVC学习笔记之json格式的输入和输出

    本篇文章主要介绍了Spring MVC学习笔记之json格式的输入和输出,这里整理了详细的代码,有需要的小伙伴可以参考下。
    2017-03-03
  • 从零实现一个简单的Spring Bean容器的代码案例

    从零实现一个简单的Spring Bean容器的代码案例

    Spring是一个非常流行的Java Web开发框架,它提供了强大的依赖注入、面向切面编程、声明式事务管理等功能,为开发者提供了高效、快速地构建Web应用程序的工具,在这篇文章中,咱们将一步一步地构建一个简单的SpringBean容器,需要的朋友可以参考下
    2023-06-06
  • java使用websocket,并且获取HttpSession 源码分析(推荐)

    java使用websocket,并且获取HttpSession 源码分析(推荐)

    这篇文章主要介绍了java使用websocket,并且获取HttpSession,通过使用配置源码分析了各方面知识点,具体操作步骤大家可查看下文的详细讲解,感兴趣的小伙伴们可以参考一下。
    2017-08-08
  • Mybatis迁移到Mybatis-Plus的实现方法

    Mybatis迁移到Mybatis-Plus的实现方法

    这篇文章主要介绍了Mybatis迁移到Mybatis-Plus的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • springboot中如何使用minio存储容器

    springboot中如何使用minio存储容器

    大家好,本篇文章主要讲的是springboot中如何使用minio存储容器,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • SpringBoot使用JavaCV处理rtsp流的示例代码

    SpringBoot使用JavaCV处理rtsp流的示例代码

    这篇文章主要为大家详细介绍了SpringBoot使用JavaCV处理rtsp流,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的小伙伴可以跟随小编一起了解一下
    2024-02-02
  • SpringBoot利用随机盐值实现密码的加密与验证

    SpringBoot利用随机盐值实现密码的加密与验证

    这篇文章主要为大家详细介绍了SpringBoot如何利用随机盐值实现密码的加密与验证,文中的示例代码讲解详细,有需要的小伙伴可以参考下
    2024-02-02
  • 一篇文章带你搞定SpringBoot中的热部署devtools方法

    一篇文章带你搞定SpringBoot中的热部署devtools方法

    这篇文章主要介绍了一篇文章带你搞定SpringBoot中的热部署devtools方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • java Swing实现弹窗效果

    java Swing实现弹窗效果

    这篇文章主要为大家详细介绍了java Swing实现弹窗效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07

最新评论