/** QueueViaArray.java ** An instance of this class represents a queue capable of holding items ** of the type specified by the client in instantiating the generic type ** parameter T. The implementation is based upon storing the queue items ** in an array. ** ** Author: R. McCloskey ** Date: January 2020 */ public class QueueViaArray implements Queue { // symbolic constant // ----------------- private static final int INIT_CAPACITY_DEFAULT = 8; // instance variables // ------------------ protected int numItems; // # items occupying the queue protected T[] items; // holds (references to) the items on the queue protected int frontLoc; // location in items[] where front item is // constructors // ------------ /* Establishes this queue to be empty. */ public QueueViaArray(int initCapacity) { numItems = 0; items = (T[])(new Object[initCapacity]); frontLoc = 0; } public QueueViaArray() { this( INIT_CAPACITY_DEFAULT); } // observers // --------- public boolean isEmpty() { return sizeOf() == 0; } public boolean isFull() { return false; } public int sizeOf() { return numItems; } public T frontOf() { return items[frontLoc]; } /* Returns a list of the (images of the) items in the queue, ** going from front to rear, separated by spaces and enclosed ** in square brackets. ** Example: "[ cat in the hat ]" */ @Override public String toString() { StringBuilder result = new StringBuilder("[ "); for (int i=0; i != numItems; i++) { result.append(items[locOf(i)] + " "); } return result.append(']').toString(); } /* Returns the location, within items[], at which the k-th element ** on the queue (counting from zero starting at the front) is stored. ** pre: 0 <= k < sizeOf() */ protected int locOf(int k) { return (frontLoc + k) % items.length; } // mutators // -------- public void clear() { // This loop is not necessary, but it could aid the garbage collector. for (int i=0; i != numItems; i++) { items[locOf(i)] = null; } numItems = 0; frontLoc = 0; // Not necessary } public void enqueue( T item ) { if (numItems == items.length) { // items[] is full, so double its length. adjustArrayLength(2 * items.length); } items[locOf(numItems)] = item; numItems = numItems + 1; } public T dequeue() { T result = items[frontLoc]; items[frontLoc] = null; // to aid garbage collection numItems = numItems - 1; frontLoc = (frontLoc + 1) % items.length; if (items.length >= 2 * INIT_CAPACITY_DEFAULT && items.length >= 4 * numItems) { // The length of items[] is at least twice the default initial // capacity and at least four times the queue's size, so cut its // length in half. adjustArrayLength(items.length / 2); } return result; } // private utility // ---------------- /* Transfers the queue elements into a new array of the specified ** length, adjusting the values of the instance variables 'items' ** and 'frontLoc' appropriately. ** pre: numItems <= newLength */ private void adjustArrayLength(int newLength) { T[] newItems = (T[])(new Object[newLength]); for (int i = 0; i != numItems; i++) { newItems[i] = items[locOf(i)]; } items = newItems; frontLoc = 0; } }