ArrayQueue.java

Download
1/*2 * ArrayQueue.java3 *4 * Computer Science S-22, Harvard University5 */6 7/*8 * A generic class that implements our Queue interface using an array.9 * We use a circular queue so that we don't need to shift items.10 */11public class ArrayQueue<T> implements Queue<T> {12    private T[] items;          // the items on the queue13    private int front;          // the index of the item at the front14    private int rear;           // the index of the item at the rear15    private int numItems;       // the number of items in the queue16    17    /*18     * Constructs an ArrayQueue object with the specified maximum size19     * for a queue that is initially empty.20     */21    public ArrayQueue(int maxSize) {22        if (maxSize <= 0) {23            throw new IllegalArgumentException("max size must be positive");24        }25        items = (T[])new Object[maxSize];26        front = 0;27        rear = -1;28        numItems = 0;29    }30    31    /* 32     * isEmpty - returns true if the queue is empty, and false otherwise33     */34    public boolean isEmpty() {35        return (numItems == 0);36    }37    38    /*39     * isFull - returns true if the queue is full, and false otherwise 40     */41    public boolean isFull() {42        return (numItems == items.length);43    }44 45    /* 46     * insert - adds the specified item at the rear of the queue.47     * Returns false if the queue is full, and true otherwise.48     */49    public boolean insert(T item) {50        if (item == null) {51            throw new IllegalArgumentException();52        } else if (isFull()) {53            return false;54        }55        rear = (rear + 1) % items.length;56        items[rear] = item;57        numItems++;58        return true;59    }60    61    /* 62     * remove - removes the item at the front of the queue and returns a63     * reference to the removed object.  Returns null if the queue is64     * empty.65     */66    public T remove() {67        if (isEmpty()) {68            return null;69        }70        T removed = items[front];71        items[front] = null;72        front = (front + 1) % items.length;73        numItems--;74        return removed;75    }76    77    /* 78     * peek - returns a reference to the item at the front of the queue79     * without removing it. Returns null if the queue is empty.80     */81    public T peek() {82        if (isEmpty()) {83            return null;84        }85        return items[front];86    }87    88    /*89     * toString - converts the queue into a String of the form 90     * {front, one-after-front, two-after-front, ...}91     */92    public String toString() {93        String str = "{";94        95        for (int i = 0; i < numItems; i++) {96            int j = (front + i) % items.length;97            str = str + items[j];98            if (i < numItems - 1) {99                str = str + ", ";100            }101        }102        103        str = str + "}";104        return str;105    }106}