import java.util.Scanner; public class MyArrayUnBndQueue { public static final int INITIAL_CAPACITY = 3; private T[] queue = (T[]) new Object[INITIAL_CAPACITY]; private int numElts = 0; private int front = 0; private int rear = queue.length-1; public T dequeue() throws Exception { if (isEmpty()) throw new Exception ("Dequeue on empty Q"); numElts--; T ans = queue[front]; front = (front+1) % queue.length; return ans; } public void enqueue(T elt) throws Exception { if (numElts == queue.length) enlarge(); numElts++; rear = (rear+1) % queue.length; queue[rear] = elt; } public boolean isEmpty() { return numElts == 0; } private void enlarge() { T[] doubleQ = (T[]) new Object[2*queue.length]; for (int i=0; i queue = new MyArrayUnBndQueue(); while (!(str=getInput.next()).equals("done")) if (str.equals("enqueue")) queue.enqueue(getInput.nextInt()); else if (str.equals("dequeue")) System.out.println(queue.dequeue()); else System.out.println("Illegal!"); } } // Local Variables: // c-basic-offset: 4 // compile-command: "javac -Xlint:unchecked MyArrayUnBndQueue.java && \ // java MyArrayUnBndQueue < input.text" // End: