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

 

相关文章:

  • 2021-11-07
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
猜你喜欢
  • 2021-05-20
  • 2021-08-07
  • 2021-09-16
  • 2021-10-23
  • 2021-10-30
  • 2022-12-23
相关资源
相似解决方案