数据结构和算法之--数组队列、环形队列

数组队列:

  1. 先进先出
  2. 入列:新数据加入队列中(在队列后面加数据)
  3. 出列:数据逐步离开队列(从头部开始离开)
  4. front:记录队列中第一个元素的位置
  5. rear:记录队列中的最后一个元素的下一个位置
  6. 入列:rear不断向后移动
  7. 出列:front不断向后移动
  8. 判断队列是否满了:rear ==  maxSize - 1(单独方法)
  9. 进行异常处理:队列的初始长度是否合法
  10. 队列添加元素
  11. 队列元素出列
  12. 判断队列是否为空
  13. 获取队列数组的长度
  14. 获取队列中元素的个数
  15. 查看第一个元素
  16. /**
     * @author yyc
     * @date 2022/7/9
     */
    public class FullQueueException extends RuntimeException{
        private static final long serialVersionUID = -4138898487109950739L;
    
        /**
         * 队列已满异常
         * @param message 异常信息
         */
        public FullQueueException(String message) {
            super(message);
        }
    }

  17. /**
     * 队列空异常
     * @author yyc
     * @date 2022/7/9
     */
    public class EmptyQueueException extends RuntimeException {
        private static final long serialVersionUID = -6511950980580054433L;
    
        /**
         * 初始化
         * @param message
         */
        public EmptyQueueException(String message) {
            super(message);
        }
    }
    import com.colin.exception.EmptyQueueException;
    import com.colin.exception.FullQueueException;
    /**
     * 数组队列
     * @author yyc
     * @date 2022/7/9
     */
    public class ArrayQueue {
        /**
         * 初始化
         * @param maxSize 队列长度
         */
        public ArrayQueue(int maxSize) {
            if (maxSize <= 0) {
                throw new IllegalArgumentException("队列长度不合理");
            }
            this.maxSize = maxSize;
            this.elementData = new String[maxSize];
        }
    
        /**
         * 记录队列其实元素的索引
         */
        private int front;
        /**
         * 记录队列最后一个元素的下一个索引
         */
        private int rear;
        /**
         * 数组长度
         */
        private int maxSize;
        /**
         * 储存值的数组
         */
        private String[] elementData;
    
        /**
         * 判断队列是否已满
         * @return true:已满
         */
        private boolean isFull() {
            return this.rear == this.maxSize - 1;
        }
    
        /**
         * 队列添加元素
         * @param element
         */
        public void add(String element) {
            if (isFull()) {
                throw new FullQueueException("队列已满,无法添数据");
            }
            this.elementData[this.rear++] = element;
        }
    
        /**
         * 判断队列是否为空
         * @return
         */
        private boolean isEmpty() {
            return this.front == this.rear;
        }
    
        /**
         * 队列元素出列
         * @return
         */
        public String get() {
            if (isEmpty()) {
                throw new EmptyQueueException("队列已空,无法出列");
            }
            String result = this.elementData[this.front];
            this.elementData[this.front++] = null;
            return result;
        }
    
        /**
         * 获取队列数组的长度
         * @return
         */
        public int queueLength() {
            return this.maxSize;
        }
    
        /**
         * 获取队列中元素的个数
         * @return
         */
        public int elementCount() {
            return this.rear - this.front;
        }
    
        /**
         * 查看第一个元素
         * @return
         */
        public String peekFirstElement() {
            return this.elementData[this.front];
        }
        @Override
        public String toString() {
            if (isEmpty()) {
                return "[]";
            }
            StringBuilder result = new StringBuilder("[");
            for (int i = this.front; ; i++) {
                result.append(this.elementData[i]);
                if (i == this.rear - 1) {
                    return result.append("]").toString();
                }
                result.append(", ");
            }
    
        }
    }

数组实现环形队列:

         

/**
 * @author yyc
 * @date 2022/7/9
 */
public class CircleArrayQueue {
    /**
     * 初始化
     * @param maxSize 队列长度
     */
    public CircleArrayQueue(int maxSize) {
        if (maxSize <= 0) {
            throw new IllegalArgumentException("队列长度不合理");
        }
        this.maxSize = maxSize;
        this.elementData = new String[maxSize];
    }

    /**
     * 记录队列其实元素的索引
     */
    private int front;
    /**
     * 记录队列最后一个元素的下一个索引
     */
    private int rear;
    /**
     * 数组长度
     */
    private int maxSize;
    /**
     * 储存值的数组
     */
    private String[] elementData;

    /**
     * 判断环形队列是否已满
     * @return
     */
    public boolean isFull() {
        return (this.rear + 1) % this.maxSize == this.front;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    private boolean isEmpty() {
        return this.front == this.rear;
    }

    /**
     * 队列添加元素
     * @param element
     */
    public void add(String element) {
        if (isFull()) {
            throw new FullQueueException("队列已满,无法添数据");
        }
        this.elementData[this.rear] = element;
        this.rear = (this.rear + 1) % maxSize;
    }
    /**
     * 队列元素出列
     * @return
     */
    public String get() {
        if (isEmpty()) {
            throw new EmptyQueueException("队列已空,无法出列");
        }
        String result = this.elementData[this.front];
        this.elementData[this.front] = null;
        this.front = (this.front + 1) % maxSize;
        return result;
    }

    /**
     * 获取队列数组的长度
     * @return
     */
    public int queueLength() {
        return this.maxSize;
    }

    /**
     * 获取队列中元素的个数
     * @return
     */
    public int elementCount() {
        return (this.rear - this.front + this.maxSize) % this.maxSize;
    }

    /**
     * 查看第一个元素
     * @return
     */
    public String peekFirstElement() {
        return this.elementData[this.front];
    }


    @Override
    public String toString() {
        if (isEmpty()) {
            return "[]";
        }
        StringBuilder result = new StringBuilder("[");

        for (int i = this.front; ; i++) {
            result.append(this.elementData[i % this.maxSize]);
            if (i == this.elementCount() + this.front - 1) {
                return result.append("]").toString();
            }
            result.append(", ");
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值