队列

在这里插入图片描述

队列

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

  • 遵循先进先出的原则

  • 数组模拟队列 front指向队列头的前一个位置,rear指向队列尾的,front会随着数据输出而改变,rear指向 初始化时rear和front都是-1 当取出数据front上移而rear不动 当front == rear(-1) 队列为空

    当rear == maxSize - 1 队列满

    image-20200924195550246

普通队列代码

/**
 * @author:zmc
 * @function:
 * @date: 2020/10/2 18:20
 */
public class Queue {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        arrayQueue.add(5);
        arrayQueue.add(4);
        arrayQueue.add(3);
        System.out.println(arrayQueue.pop());
        System.out.println(arrayQueue.pop());
        System.out.println(arrayQueue.pop());
    }
}

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 ifFull(){
        return rear == maxSize - 1;
    }

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

    public void add(int num){
        if(ifFull()){
            throw new RuntimeException("队列满");
        }
        arr[++rear] = num;
    }

    public int pop(){
        if(ifEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[++front];
    }

    public void showQueue(){
        if(ifEmpty()){
            throw new RuntimeException("队列为空");
        }
        for(int i = front+1; i <= rear; i++){
            System.out.println(arr[i]);
        }
    }

    public int head(){
        if(ifEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front+1];
    }
}

环形队列

  • front指向队列中第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值=0
  • rear指向队列的最后一个元素的后一个位置,rear的初始值=0
  • 当(rear+1) % maxSize = front,队列满
  • rear = front 队列空
  • 队列中有效数据的个数:(rear+maxSize-front)%maxSize
  • 留了一个空间不放数据
代码
/**
 * @author:zmc
 * @function:
 * @date: 2020/10/2 19:08
 */
public class CircleArrayQueue {
    public static void main(String[] args) {
        CircleArray circleArray = new CircleArray(4);
        circleArray.add(5);
        circleArray.add(4);
        circleArray.add(3);
        circleArray.showQueue();
    }
}
    class CircleArray {
        private int maxSize;
        private int front;
        private int rear;
        private int[] arr;

        public CircleArray(int arrMaxSize) {
            maxSize = arrMaxSize;
            arr = new int[maxSize];
            //指向队列头
            front = 0;
            rear = 0;
        }

        public boolean ifFull() {
            return (rear + 1) % maxSize == front;
        }

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

        public void add(int num) {
            if (ifFull()) {
                throw new RuntimeException("队列满");
            }
            arr[rear] = num;
            rear = (rear+1) % maxSize;
        }

        public int pop() {
            if (ifEmpty()) {
                throw new RuntimeException("队列为空");
            }
            int value = arr[front];
            front = (front+1) % maxSize;
            return value;
        }

        public void showQueue() {
            if (ifEmpty()) {
                throw new RuntimeException("队列为空");
            }
            for (int i = front; i < front + size(); i++) {
                System.out.println(arr[i%maxSize]);
            }
        }

        public int head() {
            if (ifEmpty()) {
                throw new RuntimeException("队列为空");
            }
            return arr[front];
        }

        public int size(){
            return (rear + maxSize - front) % maxSize;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值