CSC 132 Fall 2000
Lab 5
Due: end of today's lab, 12 October 2000
15 points
"Polymorphic Iterator"
You will need files IterLinkSeq.java and IterLinkDriver.java from the eccentric download folder for Lab 5. The IterLinkSeq class is based on the IterArrSeq example we studied in class. The differences are: IterLinkSeq uses a linked list rather than an array to contain sequence items, plus there is no backward iterator since it is pretty darned inefficient (would have to go against the flow of the links). IterLinkSeq uses a private inner class called Node, and provides a minimum of functionality (constructor, add methods, forward iterator).
As currently written, IterLinkSeq allows the client to add either a String or an int value to the sequence, and they can be mixed into the same sequence. It also has an inner class called ForwardIterator that implements the Iterator interface. The client is given a ForwardIterator object when it calls the forwardIterator method.
Your assignment is to implement the two add() methods (one for String and one for int) in the IterLinkSeq class, and to implement the constructor, hasNext and next methods for the ForwardIterator class. These should require a total of about 15 lines of code, including return statements. You are not required to document your code. Do not change any code outside these methods.
Implementation note: IterLinkSeq is specified to use a singly-linked list with a fixed header node plus tail reference. Its constructor is provided, and correctly initializes the instance variables. An empty sequence consists only of the fixed header node. Its link is initially null. A tail reference is needed for this reason: to preserve the correct sequence, new nodes must be added to the tail, not the head, of the list.
The driver adds six objects (a mix of strings and ints) into the sequence then gets two iterators and traverses both in a loop. The output is thus interleaved (f1 indicates first iterator, f2 the second). The correct output for this driver is:
f1(1) f2(1) f1(hi) f2(hi) f1(there) f2(there) f1(4) f2(4) f1(bye) f2(bye) f1(6) f2(6)
When finished, submit the modified IterLinkSeq.java file to your eccentric upload folder. Do not submit the driver.
[ Assignments | CSC 132 | Peter Sanderson | Computer Science | SMSU ]
Last reviewed: 10 October 2000
Peter Sanderson (
PeteSanderson@smsu.edu )