|
APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
The interface for priority queues as they will be used on the
Advanced Placement Computer Science exam. Exam questions
may use the PriorityQueue
interface as the type for
parameters, variables, and fields. Consider
the question below which asks to remove all items
from a priority queue that are smaller than a specified
limit and the code for the answer.
The question can be answered using just the PriorityQueue
interface.
Write the function removeLessThan
that removes
all items in parameter pq
that are strictly less than
parameter limit
. Assume all items stored in
pq
implement the Comparable
interface.
// Removes all items less than limit from pq public static void removeLessThan(PriorityQueue pq, Comparable limit) { while (! pq.isEmpty() && limit.compareTo(pq.peekMin()) > 0) { pq.removeMin(); } }
Note: there is no method size
to return the number
of elements stored in a priority queue, the AP subset/interface doesn't
include such a method.
Method Summary | |
void |
add(java.lang.Object x)
Adds an item to this priority queue. |
boolean |
isEmpty()
Returns true if this stack is empty, otherwise returns false. |
java.lang.Object |
peekMin()
Returns the smallest item stored in this priority queue without removing it. |
java.lang.Object |
removeMin()
Removes and returns the smallest item stored in this priority queue. |
Method Detail |
public boolean isEmpty()
public void add(java.lang.Object x)
x
- is the object added to this priority queuepublic java.lang.Object removeMin()
java.lang.RuntimeException
- if the priority queue is empty
(unchecked)public java.lang.Object peekMin()
java.lang.RuntimeException
- if the priority queue is empty (unchecked)
|
unofficial documentation for the APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |