import CITS2200.*; public class DequeCyclic implements Deque { private E[] items; private int first; //holds index of first item private int last; //holds index of last item private int size; //size of Queue @SuppressWarnings("unchecked") public DequeCyclic(int s) { E[] items = (E[]) new Object[s]; this.items = items; first = 0; last = 0; size = s - 1; //sets size to max index } public boolean isEmpty() { return items[first] == null; //if the first item in a Queue is null, the Queue is empty } public boolean isFull() { return first == last + 1; } public void pushLeft(E c) throws Overflow { if(!isFull()) { if(isEmpty()){items[first] = c;} //if adding to an empty Queue you don't need to change the index else { first--; if(first < 0) {first = size;} //if it goes neg, change to max index items[first] = c; } } else {throw new Overflow("Queue is Full");} } public void pushRight(E c) throws Overflow { if(!isFull()) { if(isEmpty()){items[last] = c;} else{ last = (last + 1) % items.length; //prevents it from going over the max index items[last] = c; } } else {throw new Overflow("Queue is Full");} } public E peekLeft() throws Underflow { if(!isEmpty()) {return items[first];} else {throw new Underflow("Queue is Empty");} } public E peekRight() throws Underflow { if(!isEmpty()) {return items[last];} else {throw new Underflow("Queue is Empty");} } public E popLeft() throws Underflow { if(!isEmpty()) { E RET = items[first]; items[first] = null; if(last != first) {first = (first + 1) % items.length;} //doesn't change index if Queue will be empty, otherwise go as normal. return RET; } else {throw new Underflow("Queue is Empty");} } public E popRight() throws Underflow { if(!isEmpty()) { E RET = items[last]; items[last] = null; if(last != first) { last--; if(last < 0) last = size; } //doesn't change index if Queue will be empty, otherwise go as normal. return RET; } else {throw new Underflow("Queue is Empty");} } }