Download Abstract Data Types in Java Abstract Data Types in Java

Transcript
Page 149
Figure 8-8
QEnumeration.java.
package adt.Chapter08;
import adt.Chapter06.ListEnumeration;
public class QEnumeration
extends ListEnumeration
{
public QEnumeration( QNode first )
{
super(first);
if( first == null )
throw new NullPointerException("Queue is
empty");
current = first;
}
public Object nextElement()
{
Object o = current.getData();
current = ((QNode)current).getNext();
return o;
}
}
The QEnumeration is exactly like the StackEnumeration, with the exception that it
supplies a different message string to the NullPointerException.
To test the Queue class, we need to modify the declaration in the SimpleQueueTest again
from
SimpleQueue sq = new SimpleQueue();
to
Queue sq = new Queue();
The results should be exactly like the original results for the VQueue and SimpleQueue
tests.
Some Uses for the Queue
A queue can be useful any time data needs to be handled in sequential order. Here are some
examples of how we might use a queue:
• A message queue: Messages from an outside source can be received and stored in a queue
until the application is ready to process them.
Page 150