队列Queue

队列(Queue)

——以数组的形式

  1. 特点:FIFO,先进先出。
  2. 应用:银行叫号。
  3. 要素:(三者都是index)front(前端)、rear(后端)、MaxSize-1(队列/数组容量)。
  4. 缺点:相比于环形队列,数组队列当拿出值后(getQueue、headQueue),空出来的地址不能再传值了。(如图中第三个图,0、1的位置)
  5. 代码:
    写代码的铺垫,如图:
    在这里插入图片描述

1)图中左边的队列是空的,此时,

front=-1;	//队列的第一个元素位置减一 ==0-1; -1的位置是存不了数值的,存数值从0开始
rear=-1;	//rear==front,表示了队列为空。同时,明白rear表示最后一个元素的位置

2)图中中间的队列存入了4个元素(arr[0], arr[1], arr[2], arr[3])
随着元素的增加,rear++

3)图中右边的队列释放了2个元素(arr[0], arr[1])

代码

package com.atguigu.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' '; //用于接收Scanner的输入
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.printf("输入a(addQueue),添加数据\n"
                    +"输入g(getQueue),弹出数据\n"
                    +"输入s(showQueue),展示队列\n"
                    +"输入h(headQueue),展示头元素\n"
                    +"输入e(exit),退出程序");
            key = sc.next().charAt(0);  //  key放在循环方法内部 接收输入,若放到循环外部,则运行不了其他方法
            switch (key) {
                case 'a':
                    System.out.println("输入一个数字");
                    int val = sc.nextInt();
                    queue.addQueue(val);
                    break;
                case 'g':
                    try {
                        int result = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    queue.showQueue();
                    break;
                case 'h':
                    try {
                        int result = queue.headQueue();
                        System.out.printf("头数据是%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                  sc.close();   //关掉
                  loop = false; //跳出循环
                  break;
            }
        }
    }
}

class ArrayQueue{
    private int front;
    private int rear;
    private int maxSize;
    private int[] arr;

    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        front=-1;
        rear=-1;
        arr = new int[maxSize];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return rear == maxSize;
    }

    public void addQueue(int n) {
        if (isEmpty() || !isFull()) {
            rear++;
            arr[rear] = n;
        } else {
            throw new RuntimeException("队列满,不能增加数据~");
        }
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空,不能取数据~");
        }
        front++;
        return arr[front];
    }

    public void showQueue() {
        if (!isEmpty()) {
            for (int i = 0; i < arr.length; i++) {
                System.out.printf("arr[%d]=%d\n", i, arr[i]);
            }
        } else {
            throw new RuntimeException("队列空,没有数据可以展示~");
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空,没有头元素");
        } else {
            return arr[front + 1];
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值