Java Reference
In-Depth Information
Note: Alternate names for methods
As we mentioned in Chapter 5, class designers often include aliases for certain methods. For
a queue, you could include the additional methods put and get to mean enqueue and
dequeue . The names add , insert , remove , and delete are also reasonable aliases. Likewise,
you could provide a method peek to mean getFront .
10.3
The Java interface in Listing 10-1 specifies a queue of objects. The generic type T —which can be
any class type—represents the data type of the items in the queue.
LISTING 10-1
An interface for the ADT queue
public interface QueueInterface<T>
{
/** Adds a new entry to the back of the queue.
@param newEntry an object to be added */
public void enqueue(T newEntry);
/** Removes and returns the entry at the front of this queue.
@return either the object at the front of the queue or, if the
queue is empty before the operation, null */
public T dequeue();
/** Retrieves the entry at the front of this queue.
@return either the object at the front of the queue or, if the
queue is empty, null */
public T getFront();
/** Detects whether this queue is empty.
@return true if the queue is empty, or false otherwise */
public boolean isEmpty();
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface
10.4
Example: Demonstrating the queue methods. The following statements add, retrieve, and
remove strings from a queue. We assume that the class LinkedQueue implements QueueInterface
and is available.
QueueInterface<String> myQueue = new LinkedQueue<String>();
myQueue.enqueue("Jim");
myQueue.enqueue("Jess");
myQueue.enqueue("Jill");
myQueue.enqueue("Jane");
myQueue.enqueue("Joe");
String front = myQueue.getFront(); // returns "Jim"
System.out.println(front + " is at the front of the queue.");
front = myQueue.dequeue(); // removes and returns "Jim"
System.out.println(front + " is removed from the queue.");
myQueue.enqueue("Jerry");
 
Search WWH ::




Custom Search