《尚硅谷数据结构》-数组模拟队列

目录

队列的介绍

数组模拟队列

数组模拟环形队列


队列的介绍

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

队列是先入先出,即先存入队列的数据,要先取出。

数组模拟队列

使用数组模拟队列示意图—第一个是初始队列,第二个是存数据(从上面进),第三个是取数据(从下面出)

(但是这样是一次性的队列)

MaxSize是队列的最大容量,变量rear会随着数据的输入而改变,而变量front会随着数据的输出而改变。

// 使用数组模拟队列-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()){
            // 抛出异常,因为不能返回
            throw new RuntimeException("队列为空,没有数据");
        }
        return arr[front+1];
    }
}

存在问题:

这样构建的是一次性队列,当队列添加满后,就算把队列的原来值取出来了,也无法给队列添加新的数字。

数组模拟环形队列

思路调整:

  1. front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素 front 的初始值 = 0
  2. rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置因为空余一位作为约定队尾rear 的初始值 = 0   (空出的1个空间用来区分队空和队满
  3. 当队列满时,条件是 (rear + 1) % maxSize == front 【满】  (其实是 rear %(maxSize-1)== front)
  4. 对队列为空的条件, rear == front 空
  5. 当我们这样分析, 队列中有效的数据的个数 (rear + maxSize - front) % maxSize 

 

// 使用数组模拟队列-CircleQueue
class CircleQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    // 创建队列的构造器
    public CircleQueue(int arrmaxSize){
        maxSize = arrmaxSize;
        arr = new int[maxSize];
        front = 0; //队列的第一个元素
        rear = 0; //队列的最后一个元素的后一个位置
    }

    //判断队列是否满
    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+1) % maxSize; //取模 配合环形结构
    }

    //从队列中取出数据
    public int getQueue(){
        if(isEmpty()){
            // 抛出异常,因为不能返回
            throw new RuntimeException("队列为空,不能取出数据");
        }
        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+(rear+maxSize-front)%maxSize; i++){
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }

    //显示队列的头数据
    public int headQueue(){
        if(isEmpty()){
            // 抛出异常,因为不能返回
            throw new RuntimeException("队列为空,没有数据");
        }
        return arr[front%maxSize];
    }
}
// 测试队列程序
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        CircleQueue Queue = new CircleQueue(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):显示队列头的数据");
            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());
                    }//这里用try-catch是为了不影响后面的操作,抓住之前抛出的异常,不然程序就直接结束了
                    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("程序已退出");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值