/** * A class file that is used for creating a Double Ended Queue, or deque. * It contains all methods necessary for working with the deque. * It contains wrap-around support and uses arrays. * * @author Oleg Fortun */public class Deque{ private int maxSize; // The max size of the deque private String[] queArray; private int right; // reference for insertions on the right private int left; // reference for insertions on the left private int nItems; // the number of items /** * The constructor for creating the deque. * It takes the maxSize and creates a String array. * It sets right at the front of the deque. * It sets left at the rear of the deque. * It sets the number of items to 0. * @param size The size of the initial deque. */ public Deque(int size) // constructor { maxSize = size; // size of the deque queArray = new String[maxSize]; // create new String array right = 0; // set right at front left = maxSize-1; // set left at rear nItems = 0; // set nItems to 0 } // end Deque() /** * Inserts an item left of the current left pointer. * Checks if the queue is full, if it is returns false. * Checks if wraparound is required a d adjusted accordingly. * Sets the appropriate index to the String passed. * Incremets the number of items. * @param item String value to be inserted * @return true if successful, false if full. */ public boolean insertLeft(String item) // put item at left of queue { if ( isFull() ) // if deque full, cannot insert anymore return false; if(left == -1) // deal with wraparound left = maxSize-1; queArray[left--] = item; // insert and shifts left down nItems++; // incremement number of items return true; } // end insertLeft() /** * Inserts an item right of the current right pointer. * Checks if the queue is full, if it is returns false. * Checks if wraparound is required a d adjusted accordingly. * Sets the appropriate index to the String passed. * Incremets the number of items. * @param item String value to be inserted * @return true if successful, false if full. */ public boolean insertRight(String item) // put item at left of queue { if ( isFull() ) // if deque full, cannot insert anymore return false; if(right == maxSize)// deal with wraparound right = 0; queArray[right++] = item; // insert and shifts right up nItems++; // incremement number of items return true; } // end insertRight() /** * Removes the item previous of the current left pointer * (insertLeft increments it after insertion). * Checks if the deque is empty, if it is returns an error message. * Increments the left pointer to the actual value to remove. * Checks if the new left pointer requires wraparound, and does so. * Sets the value to delete as temp, for return purposes. * Sets the value to delete as null. * Decrements the number of items. * @return The value that has been deleted. */ public String removeLeft() // remove item at left pointer { if ( isEmpty() ) // if deque empty, cannot remove return "No items to remove."; left++; // increment pointer to actual value if(left == maxSize) // deal with wraparound left = 0; String temp = queArray[left]; // store value to be removed in temp queArray[left] = null; // set value to null nItems--; // decremement number of items return temp; // return item removed } // end removeLeft() /** * Removes the item previous of the current right pointer * (insertRight increments it after insertion). * Checks if the deque is empty, if it is returns an error message. * Decrements the right pointer to the actual value to remove. * Checks if the new right pointer requires wraparound, and does so. * Sets the value to delete as temp, for return purposes. * Sets the value to delete as null. * Decrements the number of items. * @return The value that has been deleted. */ public String removeRight() // remove item at right pointer { if ( isEmpty() ) // if deque empty, cannot remove return "No Items to remove."; right--; // decrement pointer to actual value if(right == -1) // deal with wraparound right = (maxSize-1); String temp = queArray[right]; // store value to be removed in temp queArray[right] = null; // set value to null nItems--; // decremement number of items return temp; // return item removed } // end removeRight() /** * Returns whether or not the deque is empty. * @return true if empty, false if not. */ public boolean isEmpty() // true if queue is empty { return (nItems==0); } // end isEmpty() /** * Returns whether or not the deque is full. * @return true if full, false if not. */ public boolean isFull() // true if queue is full { return (nItems==maxSize); } // end isFull()} // end class Deque