import java.util.*; /** *This is my own work. * Circular Array Deque: Intended to demonstrate the array implementation of * the Double-ended queue interface, such that elements can be added and removed * from either end. * @author jcsupple (Craig Supples) */ class jcsupple_hwk2 { /** * Empty constructor, sets default values for items, back, front, * currentSize, and capacity; * @param none * @return new jcsupple_hw2/Circular array deque object */ public jcsupple_hwk2() { items = (E []) new Object[DEFAULT_CAPACITY]; back = -1; front = 0; currentSize = 0; capacity = DEFAULT_CAPACITY; } /** * Constructor using an existing Collection that extends E * @param other (Collection that extends E) * sets initial values as determined by other */ public jcsupple_hwk2(Collection other) { items = (E []) other.toArray(); capacity = currentSize = other.size(); front = 0; back = capacity-1; } /** * returns the size of the deque * @return currentSize */ public int size() { return currentSize; // if front< back, back-front // if front> back, (items.length-front) + back } /** * Determines if it's empty, used before the execution of a removal/get * @return true if Empty and false if not */ public boolean isEmpty() { return currentSize == 0; } /** * This returns element at front index * @return element at the front * if empty, returns null */ public E getFirst() { if(!isEmpty()) { return items[front]; } else return null; } /** * adds an element e to the front of the array, if full it doubles * @param e (unspecified element) */ public void addFirst(E e) { if (currentSize == capacity) { doubleDeque(); } front = decrement(front); items[front] = e; currentSize++; } /** * removes the element at front * @return removed element, if empty, returns null */ public E removeFirst() { if(!isEmpty()) { E placeholder = items[front]; front = increment(front); currentSize--; return placeholder; } else return null; } /** * returns element at back * @return element at back, null if empty */ public E getLast() { if(!isEmpty()) { return items[back]; } else return null; } /** * adds element e at back, if full it doubles * @param e (unspecified element) */ public void addLast(E e) { if(currentSize == capacity) { doubleDeque(); } back = increment(back); items[back] = e; currentSize++; } /** * removes element at back * @return removed element, if empty, returns null */ public E removeLast() { if(!isEmpty()) { E placeholder = items[back]; back = decrement(back); currentSize--; return placeholder; } else return null; } /** * returns element at specified index in relation to front * @param idx index of element if front is 0 * @return element at index idx in relation to front */ public E get(int idx) { return items[(front+idx)% capacity]; } /** * increments x and wraps around if over capacity * @param x * @return x, wrapped around if necessary */ private int increment(int x) { if(++x == items.length) { x=0; } return x; } /** * decrements x, wraparound if necessary * @param x * @return x, wrapped around if necessary */ private int decrement(int x) { if(--x == -1) x = items.length-1; return x; } /** * increases the capacity of items by doubling the capacity */ private void doubleDeque() { E[] temp = (E []) new Object[capacity];//temp array to hold items w/o wrap for(int i = 0, j = front; i