package DataStructures;
publicclass QueueArray
{ staticfinalint DEFAULT_CAPACITY = 10; private Object[] theArray; privateint currentSize; privateint front; privateint back; /** * Construct the queue. * */ public QueueArray() { this(DEFAULT_CAPACITY); } /** * Construct the queue. * * @param capacity */ public QueueArray(int capacity) { theArray = new Object[capacity]; MakeEmpty(); } /** * Make the queue logically empty. * */ publicvoid MakeEmpty() { currentSize = 0; front = 0; back = -1; } publicboolean IsEmpty() { return currentSize == 0; } publicboolean IsFull() { return currentSize == theArray.length; } /** * Get the least recently inserted item in the queue. Does not alter the * queue. * * @return the least recently inserted item in the queue, or null, if empty. */ public Object getFront() { if (IsEmpty()) returnnull; return theArray[front]; } /** * Insert a new item into the queue. * * @param x * the item to insert. * @throws Overflow * if queue is full. */ publicvoid Enqueue(Object x) throws Overflow { if (IsFull()) thrownew Overflow(); back = increment(back); theArray[back] = x; currentSize++; } /** * Internal method to increment with wraparound. * * @param x * any index in theArray`s range. * @return x+1,or 0,if x is at the end of theArray; */ privateint increment(int x) { if (++x == theArray.length) x = 0; return x; } /** * Return and remove the least recently inserted item from the queue. * * @return the least recently inserted item, or null, if empty. */ public Object Dequeue() { if (IsEmpty()) returnnull; currentSize--; Object frontItem = theArray[front]; theArray[front] = null; front = increment(front); return frontItem; } }