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