Java Reference
In-Depth Information
11.18
An outline of the class. The class that implements the queue begins by declaring a vector as a data
field and allocating the vector in its constructors, as Listing 11-3 shows.
LISTING 11-3
An outline of a vector-based implementation of the ADT queue
import java.util.Vector;
/**
A class that implements a queue of objects by using a vector.
@author Frank Carrano
*/
public class VectorQueue<T> implements QueueInterface<T>
{
private Vector<T> queue; // queue's front entry is first in the vector
public VectorQueue()
{
queue = new Vector<T>(); // vector doubles in size if necessary
} // end default constructor
public VectorQueue( int initialCapacity)
{
queue = new Vector<T>(initialCapacity);
} // end constructor
< Implementations of the queue operations go here. >
. . .
} // end VectorQueue
11.19
Adding to the back. We use Vector 's method add to add an entry to the end of the vector—that is,
to the back of the queue.
public void enqueue(T newEntry)
{
queue.add(newEntry);
} // end enqueue
11.20
Retrieving the front entry. We retrieve the entry at the front of the queue by using Vector 's
method get with an argument of zero:
public T getFront()
{
T front = null ;
if (!isEmpty())
front = queue.get(0);
return front;
} // end getFront
 
Search WWH ::




Custom Search