|
APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 |
public boolean isEmpty()
public void enqueue(java.lang.Object x)
x
- is the object enqueued onto this queue.public java.lang.Object dequeue()
java.lang.RuntimeException
- if this queue is empty (unchecked)peekFront()
public java.lang.Object peekFront()
java.lang.RuntimeException
- if this queue is empty (unchecked)dequeue()
|
unofficial documentation for the APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |