
public class Quiz08 {

  /** Reverses the elements stored in the given Queue. */
  public static <E> void reverse(Queue<E> q) {
    Stack<E> s = new Stack<E>();
    //empty the queue into a stack
    while (q.size() > 0) {
      s.push(q.poll());
    }
    //empty the stack back into the original queue
    while (s.size() > 0) {
      q.offer(s.pop());
    }
  }

  /** Demonstrates the reverse method in action. */
  public static void main(String[] args) {
    Queue<String> q = new Queue<>();
    for (String s : new String[]{"A", "B", "C", "D"}) {
      q.offer(s);
    }
    System.out.println(q);  // [A, B, C, D]
    reverse(q);
    System.out.println(q);  // [D, C, B, A]
  }
}
