next up previous
Next: Worksheet 3 Up: Worksheets Previous: Worksheet 1

Worksheet 2

Modify the Queue interface so that it includes a method

public Enumeration elements();
which returns an enumeration of the elements currently in the queue, beginning with the head of the queue (the next one to be dequeued). Write the code for this method using the QueueArray implementation of queues.

You will find it helpful to look at the elements method in the notes on the LinkedList class (we first implemented an Enumeration and then in the final version an Iterator). That will give your a hint of the general structure of the code.

You are given three files that will help.

MyQueue.java specifies the interface you should implement
MyQueueArray.java contains all the implementation except for elements()
MyQueueDemo.java a test program
Copy these three files to your area and work on the implementation file MyQueueArray.java. You just need to fill in the code for the MyQueueArray.elements method.

Try your code out by running the demo program in MyQueueDemo.java. The output should be

enqueue:   82 50 76 89
enumerate: 82 50 76 89
dequeue:   82
enumerate: 50 76 89
enqueue:   95 57
enumerate: 50 76 89 95 57

Lists

Do the same thing for the QueueList implementation of queues--in fact it's easier. You now need MyQueueList.java, which contains all the implementation except for the elements method, and ListNode.java. The MyQueue interface file will be the same as before. The MyQueueDemo file just needs to have a different line commented out near the top.


next up previous
Next: Worksheet 3 Up: Worksheets Previous: Worksheet 1
Peter Williams 2005-06-07