zoukankan      html  css  js  c++  java
  • 数组实现队列

    /**
     * @author:liuxincheng
     * @description:
     * @date:created in 2019/1/22 13:49
     * @modified by liuxincheng
     */
    
    public class ArrayQueue<T> {
        /**
         * 队列数组
         */
        private T[] queue;
        /**
         * 头下标
         */
        private int head;
        /**
         * 尾下标
         */
        private int tail;
        /**
         * 元素个数
         */
        private int count;
    
        public ArrayQueue() {
            queue = (T[]) new Object[10];
            // 头下标为零
            this.head = 0;
            this.tail = 0;
            this.count = 0;
        }
    
        public ArrayQueue(int size) {
            queue = (T[]) new Object[size];
            this.head = 0;
            this.tail = 0;
            this.count = 0;
        }
    
        /**
         * 入队
         *
         * @param t
         * @author: liuxincheng
         * @date: 2019/1/22 13:53
         * @return: boolean
         */
        public boolean inQueue(T t) {
            if (count == queue.length) {
                return false;
            }
            // 如果不为空就放入下一个
            queue[tail++ % (queue.length)] = t;
            count++;
            return true;
        }
    
        /**
         * 出队
         *
         * @param
         * @author: liuxincheng
         * @date: 2019/1/22 13:54
         * @return: T
         */
        public T outQueue() {
            // 如果是空的那就不能再出栈了
            if (count == 0) {
                return null;
            }
            count--;
            return queue[head++ % (queue.length)];
        }
    
        /**
         * 查队列
         *
         * @param
         * @author: liuxincheng
         * @date: 2019/1/22 13:55
         * @return: T
         */
        public T showHead() {
            if (count == 0) {
                return null;
            }
            return queue[head];
        }
    
        /**
         * 判满
         *
         * @param
         * @author: liuxincheng
         * @date: 2019/1/22 13:56
         * @return: boolean
         */
        public boolean isFull() {
            return count == queue.length;
        }
    
        /**
         * 判空
         *
         * @param
         * @author: liuxincheng
         * @date: 2019/1/22 13:56
         * @return: boolean
         */
        public boolean isEmpty() {
            return count == 0;
        }
    }
  • 相关阅读:
    JavaScript的for循环
    javaScript的执行机制-同步任务-异步任务-微任务-宏任务
    js排他性算法
    js倒计时
    微信小程序 简单获取屏幕视口高度
    小程序scroll-view实现回到顶部
    Vue使用js鼠标蜘蛛特效
    小程序获取当前播放长度和视频总长度,可在播放到某一时长暂停或停止视频
    Django学习笔记
    SQLite简单介绍
  • 原文地址:https://www.cnblogs.com/lxcmyf/p/10303483.html
Copyright © 2011-2022 走看看