APCS Java Subset

ap
Interface Queue

All Known Implementing Classes:
ListQueue

public interface Queue

The interface for queues as they will be used on the Advanced Placement Computer Science exam. Exam questions may use the Queue interface as the type for parameters, variables, and fields. Consider the question below which asks to reverse the elements stored in a queue and the code for the answer. The question can be answered using just the Queue interface.

Write the function reverseQueue that reverses the order of the items in parameter q. If q stores (q0, q1, ..., qn-1) before the function is called with q0 as the first element of q, then q stores (qn-1, ... q1, q0) after the function executes, where q0 is now the last element of q.

 // Reverses the order of the items stored in parameter q

 public static void reverseQueue(Queue q)
 {
     ArrayList list = new ArrayList();
     while (! q.isEmpty()) {
         list.add(q.dequeue());
     }
     for(int k=list.size()-1; k >= 0; k--) {
         q.enqueue(list.get(k));
     }
 }
 

Note: there is no method size to return the number of elements stored in a queue, the AP subset/interface doesn't include such a method.


Method Summary
 java.lang.Object dequeue()
          Dequeues and returns the first element of the queue.
 void enqueue(java.lang.Object x)
          Enqueue an element onto the back of this queue.
 boolean isEmpty()
          Returns true if this queue is empty, otherwise returns false.
 java.lang.Object peekFront()
          Returns the first element of the queue without dequeuing it.
 

Method Detail

isEmpty

public boolean isEmpty()
Returns true if this queue is empty, otherwise returns false.

Returns:
true if this queue is empty, otherwise return false.

enqueue

public void enqueue(java.lang.Object x)
Enqueue an element onto the back of this queue.

Parameters:
x - is the object enqueued onto this queue.

dequeue

public java.lang.Object dequeue()
Dequeues and returns the first element of the queue.

Returns:
the just-dequeued element of the queue
Throws:
java.lang.RuntimeException - if this queue is empty (unchecked)
See Also:
peekFront()

peekFront

public java.lang.Object peekFront()
Returns the first element of the queue without dequeuing it.

Returns:
the first element of the queue
Throws:
java.lang.RuntimeException - if this queue is empty (unchecked)
See Also:
dequeue()

unofficial documentation for the APCS Java Subset