数组实现队列

/**
 * @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;
    }
}

 

posted @ 2019-01-22 14:03  如果屈原会编程  阅读(347)  评论(0编辑  收藏  举报