队列

作者: 桑鱼nicoo | 来源:发表于2020-02-18 20:39 被阅读0次

    队列是一个有序列表,遵循先入先出的原则。即先存入队列的数据,要先取出。后存入的要后取出。可以用数组或是链表来实现。队列最形象的比喻是:公车排队问题,先排队的要先上车,后排队的后上车。

    用数组的方式实现队列

    public class ArrayQueueDemo {
        public static void main(String[] args) {
            // 测试
            ArrayQueue queue = new ArrayQueue(3);
            queue.addQueue(1);
            queue.addQueue(2);
            queue.addQueue(3);
            queue.showQueue();
            int i = queue.headQueue();
            System.out.println(i);
            int res = queue.getQueue();
            System.out.println("res " + res);
            queue.showQueue();
    
            int res1 = queue.getQueue();
            System.out.println("res " + res1);
            queue.showQueue();
        }
    }
    
    class ArrayQueue {
        private int maxSize; // 数组的最大容量
        private int front; // 队列头
        private int rear; // 队列尾
        private int[] arr; // 数组用于存放数据,模拟队列
    
        /**
         * 创建队列的构造器
         *
         * @param arrMaxSize
         */
        public ArrayQueue(int arrMaxSize) {
            maxSize = arrMaxSize;
            arr = new int[maxSize]; // 指定队列的最大长度,最大下标是maxSize-1
            front = -1; // 指向队列的头部,初始值是-1,每次取出数据的时候先+1
            rear = -1; // 指向队列的尾部,初始值为-1,每次添加一个值+1
        }
    
        /**
         * 判断队列是否满
         *
         * @return
         */
        public Boolean isFull() {
            return rear == maxSize - 1; // rear指向队列的尾部,与队列的最大下标(maxSize - 1)相比,相等则是队列已经满了
        }
    
        /**
         * 判断队列是否为空
         *
         * @return
         */
        public Boolean isEmpty() {
            return rear == front; // rear的最大值是maxSize-1,如果大于则已经满了,如果和front相等,要么在对头都是-1,要么已经取完值了
        }
    
        /**
         * 添加数据到队列
         *
         * @param n
         */
        public void addQueue(int n) {
            if (isFull()) { // 添加数据队列前要判断是是否队列满了
                System.out.println("队列满,不能加入数据");
                return;
            }
    
            rear++; // 每次添加前+1
            arr[rear] = n;
        }
    
        /**
         * 获取队列的数据,出队列
         * @return
         */
        public int getQueue() {
            if(isEmpty()){
                throw new RuntimeException("队列空,不能获取数据");
            }
            front++; // 每次取值前+1,因为的取值后那个位置是空的,所以下次取值前要加+1
            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]);
            }
        }
    
        /**
         * 显示队列的头数据,注意不是取出数据
         * @return
         */
        public int headQueue(){
            if(isEmpty()){ // 查看队列、取出数据前要判断是否是空的
                throw new RuntimeException("队列空的,没有数据");
            }
            return arr[front+1];
        }
    }
    

    相关文章

      网友评论

          本文标题:队列

          本文链接:https://www.haomeiwen.com/subject/ezwzxhtx.html