• Java队列


    在这里插入图片描述

    数组模拟队列

    在这里插入图片描述

    public class 数组模拟队列 {
        public static void main(String[] args) {
            ArrayQueue arrayQueue = new ArrayQueue(2);
            arrayQueue.addQueue(1);
            arrayQueue.addQueue(10);
            System.out.println(arrayQueue.getQueue());
            System.out.println(arrayQueue.hearQueue());
            System.out.println(arrayQueue.getQueue());
            System.out.println(arrayQueue.hearQueue());
    
        }
    }
    class ArrayQueue{
        //数组最大容量
        private int maxSite;
        //队列头
        private int front;
        //队列尾
        private int rear;
        //存放数据模拟队列
        private int[]arr;
        //创建队列的构造器
        public ArrayQueue(int arrMaxSize){
            maxSite=arrMaxSize;
            //数组赋值长度
            arr = new int [maxSite];
            front=-1;//初始化头部
            rear=-1;//初始化尾部
        }
        //判断队列是否已满
        public boolean isFull(){
            return rear == maxSite-1;
        }
        //判断队列是否为空
        public boolean isEmpty(){
            return rear == front;
        }
        //添加数据到队列
        public void  addQueue(int n){
            //判断队列是否已满
            if (isFull()){
                System.out.println("队列已满");
                return;
            }
            rear++;
            arr[rear]=n;
        }
        //显示队列的全部数据
        public void showQueue(){
            if (isEmpty()){
                System.out.println("队列为空");
                return;
            }
            for (int i=0;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    Java中的队列方法

            //Java方法实现队列
            Queue queue = new LinkedList();
            //添加元素
            queue.offer("123");
            queue.offer("321");
            queue.offer("213");
            for (String q:queue) {
                System.out.println(q);
            }
            //返回第一个元素然后从队列删除
            System.out.println(queue.poll());
            //返回第一个元素不删除
            System.out.println(queue.element());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    Excel自学三部曲_Part3:Excel工作场景分析实战
    基于MFC——C++课程设计《学生信息管理系统》
    xindi-2022-08-23数据分析记录
    浅谈构造函数【Javascript】
    基于数字孪生的车路协同虚拟仿真平台研究
    细胞膜包裹氧化物纳米复合物/金纳米粒子/笼/仿生细胞膜-内核纳米粒子的制备
    基于 .NET 7 的 QUIC 实现 Echo 服务
    【数据库开发】DataX开发环境的安装部署(Python、Java)
    迅为IMX6开发板QT系统LVDS和HDMI双屏异显和同显
    每天学一个MySQL函数(二):CONCAT_WS
  • 原文地址:https://blog.csdn.net/qq_45007567/article/details/133779494