// During enqueue, this queue shifts all objects // to the left. This can takes a noticable amount of // time when the size of the queue is big. public class SlowQueue implements GenericQueue { private int back; private Object[] my_data; // In a non-empty queue, front element is always at my_data[0] public final static int CAPACITY = 1000; public SlowQueue() { my_data = new Object[CAPACITY]; back = -1; } public SlowQueue(int max) { my_data = new Object[max]; back = -1; } public void enqueue(Object element) { back++; my_data[back] = element; } public E dequeue() { // Store the front element before erasing it in the shift loop E temp = (E) my_data[0]; // Shift all element down the array. This is very inefficient. for (int j = 0; j < back; j++) { my_data[j] = my_data[j + 1]; } // Explicitly write that the old location of back is set for reuse my_data[back] = null; // Decrement back so the next enqueue is in the correct place back--; return temp; } public boolean isEmpty() { return back < 0; } public E peek() { return (E) my_data[0]; } }