270 Chapter 13: Collections
Queue Manipulation
The file QueueTest.java contains a printQueue method that takes an object of type QueueADT and prints its
contents, restoring the queue before it returns. It uses a temporary queue that actually holds the same
information as the original queue. If you know the number of elements in the queue, you can write a printQueue
method that prints the queue and restores it to its original form without using an auxiliary data structure (stack,
queue, etc.). Think about how, then do it! That is, modify the printQueue method in QueueTest so that it
behaves exactly as it does now but does not require an auxiliary data structure. Note that this code uses a
LinkedQueue implementation for the QueueADT (see previous exercises), but you could substitute an
ArrayQueue if you like.
// **************************************************************
// QueueTest.java
//
// A simple driver to manipulate a queue.
//
// **************************************************************
public class QueueTest
{
public static void main(String[] args)
{
QueueADT queue = new LinkedQueue();
//put some stuff in the queue: 0,2,4,..,14
//dequeue 4 items
for (int i=0; i<4; i++)
}
//———————————————————-
// Prints elements of queue, restoring it before returning
//———————————————————-
public static void printQueue(QueueADT queue)
{
QueueADT temp = new LinkedQueue();
//print everything in the queue, putting elements
//back into a temporary queue
while (!queue.isEmpty())
{