262 Chapter 13: Collections
An Array Queue Implementation
File QueueADT.java contains a Java interface representing a queue ADT. In addition to enqueue(), dequeue(),
and isEmpty(), this interface contains two methods that are not described in the book – isFull () and size(). File
ArrayQueue.java contains a skeleton for an array-based implementation of this interface; it also includes a
toString() method that returns a string containing the queue elements, one per line. File TestQueue.java contains
a simple test program.
Complete the method definitions in ArrayQueue.java. Some things to think about:
• A queue has activity at both ends — elements are enqueued at one end and dequeued from the other
end. In an array implementation this means that repeated enqueues and dequeues will shift the queue
dequeue methods to be implemented efficiently in both space and time.
• You’ll need to use integers to keep track of the indices of the front and back of the queue. Think
carefully about what initial values these variables (front and back) should get in the constructor and
how they should be incremented given the circular nature of the implementation.
• The easiest way to implement the size() method is to keep track of the number of elements as you go
with the numElements variable — just increment this variable when you enqueue an element and
decrement it when you dequeue an element.
• An easy way to tell if a queue is full in an array implementation is to check how many elements it
Study the code in TestQueue.java so you know what it is doing, then compile and run it. Correct any problems
in your Linked Queue class.
//**********************************************************
// QueueADT.java
// The classic FIFO queue interface.
//**********************************************************
public interface QueueADT
{
//———————————————
// Returns true if queue is empty.
//———————————————
public boolean isEmpty();