ListIterators for the MyArrayList and MyLinkedList classes.
The interface for ListIterator can be found
here. To get this right, you will need to
take the following into account (the discussion is in terms of the linked
list class, but it's essentially the same as for the array list class):
MyLinkedList (which it's tempting, and reasonable, to call
LinkedListIterator) that implements the
java.util.ListIterator interface. Make especial note of the additional methods that you must implement and their behavior according to the Java API specification.MyLinkedList to
have a method named iterator() that returns a plain old Iterator. It should, of course, have a method named listIterator that returns a
ListIterator! The problem is, the
Iterable interface (which MyLinkedList implements)
requires the presence of an iterator() method. What interface requires the presence only of a listIterator() method?
The answer (as I'm sure you all know :-) is actually not an interface
at all,
but rather the abstract class
AbstractSequentialList.
Put as simply as possible, the upshot is that MyLinkedList
should no longer implement Iterable<AnyType>,
but rather extend
AbstractSequentialList<AnyType>, and the method iterator() should be replaced by
(an appropriately functioning) listIterator().
(AbstractSequentialList puts no further constraints on your
code; listIterator() is the only abstract (in other
words, unimplemented) method in the class.)
balanced()) in a class
that also includes a constructor
that takes a string of parentheses as a parameter, and a toString()
method that returns the expression.MyArrayList and one using MyLinkedList. We'd like to compare the run-time of these two. And we'll do that in two ways as well,
both empirically and theoretically. We'll be doing stuff like this in
future assignments, so this is a good time to look into it. It's not difficult to time stuff in Java. Maybe you know a better way, but here's one thing you can do. Suppose there's some code you want to time. Then you sandwich it
with several lines of code on each end as displayed here:
Calendar rightNow = Calendar.getInstance();
int initialTime = rightNow.get(Calendar.MILLISECOND);
initialTime += rightNow.get(Calendar.SECOND)*1000;
initialTime += rightNow.get(Calendar.MINUTE)*60*1000;
//Code you want to time
rightNow = Calendar.getInstance();
int finalTime = rightNow.get(Calendar.MILLISECOND);
finalTime += rightNow.get(Calendar.SECOND)*1000;
finalTime += rightNow.get(Calendar.MINUTE)*60*1000;
System.out.println("Elapsed time = " + (finalTime - initialTime) + " milliseconds");
So... write a driver program and input some large expressions of balanced parentheses
to test and compare the run-times of the algorithms based on the two data structures.
You should find a significant difference! (Actually, to see that significant a difference, you may need very long expressions.
I'll give you one here. Typing this in at the command line may not work, so just hard-code it into the program.)