600.226 Data Structures -- Spring 2013
Homework #6
Part A) Read and Review
The main purpose of this assignment is to gain experience
implementing and using linked lists and iterators. Read up on
linked lists, Generics, Iterators, and the Collections
API in Java. Lots of resources are posted on
the detailed schedule.
Part B) Short Answer [30 points] -- Due 2:30pm on Monday, 3/4
- [8] Write a non-recursive algorithm, using pseudocode only, to
print a singly linked list (with a head reference, but no tail
pointer) in reverse, using only constant extra space (ie, no copying
to an array). Assume the method is a member of a singly linked list
class with nested Node class (much like our SLList implementation).
What is the running time of your algorithm?
- [6] Write a recursive method, in pseudocode only, to print a
singly linked list in reverse. Assume that the head of the list (a
Node) is passed as a parameter to this method. What is the running
time of your algorithm on a list with N nodes? How much space is used
in addition to the list itself (tricky - think carefully)?
- [8] Look at LinkedList and ListIterator in the Java API. Then
do a hand trace of this code and draw the entire linked list as it exists in
memory at each point marked with /* DRAW */.
LinkedList l = new LinkedList< String >();
l.addFirst("aaa");
l.addLast("bbb");
l.addFirst("ccc");
l.addFirst("ddd");
/* DRAW A */
ListIterator iter = l.listIterator();
iter.next();
iter.add("eee");
iter.add("fff");
iter.next();
/* DRAW B */
iter.next();
iter.remove();
iter.previous();
iter.add("ggg");
/* DRAW C */
iter.previous();
iter.remove();
iter.next();
iter.add("hhh");
/* DRAW D */
- This question refers to the Java API interfaces and classes.
Suppose you have a List L of items and a SortedSet S of positive
integers in descending order.
- [5] Using only methods available through those (List and
SortedSet) API interfaces (including their super-interfaces), write
the body of this generic static method printSelect(L,S) that prints
only the items in L that are in positions corresponding to the index
values in S. Try to be as efficient as possible.
public static <T> void printSelect(List<T> L, SortedSet<Integer> S)
- [1] What is the running time of your method if L and S are implemented
with Java ArrayLists?
- [1] What is the running time of your method if L and S are
implemented with Java LinkedLists?
- [1] Can either be improved by implementing the printSelect method
differently?
Part C) Implementation [55 pts] -- due by 2:30 pm on Wednesday, 3/6
For the purpose of this assignment, we have defined an
BoundedOrderedSet interface, which represents a naturally ordered
container of unique elements, which may be bounded below and above.
In other words, no duplicates are allowed, elements must Comparable,
and an inclusive minimum and/or exclusive maximum may be set as
restrictions on allowable elements. Furthermore, whenever the
OrderedSet is displayed or iterated through, the items are in order.
Our interface extends the Iterable Java interface, as well as our
own generic Set interface from homework5. However, the way that the
Set and Iterable methods are implemented will differ from the
SetArray implementation because the set is now Ordered, and
Iterable, and potentially Bounded, and also because you can not use
an array based implementation.
[30 pts] Write BoundedOrderedSetList.java - a linked list
implementation of
our BoundedOrderedSet.java
interface. There are several approaches one could take for this
problem, alone or in combination:
- keep items always sorted in the Set
- use iterators to implement most methods and sort when the iterator is initialized
- extend BagiList, add Set specific methods and override others
as necessary
- use an SLList (our implementation) within your BoundedOrderedsetList
- write all methods from scratch (you'll learn the most)
- use sentinel (dummy) nodes for your linked list
- use single or doubly linked list
[15 pts] You are required to choose the first approach: keep
items always ordered in every set. This precludes choosing the
second approach, but can be used in combination with the remaining
options. Write a short paragraph explaining why keeping the items
always ordered is a better approach than sorting every time you need
to iterate over the set. Also you are required to comment on the
running times of all methods again.
[10 pts] You must make your BoundedOrderedSetList.java checkstyle compliant.
You must also compile with the -Xlint:all option to turn on all warnings and
may not have any in your final submission. [From the command line:
javac -Xlint:all myProgram.java - look up how to set this in whatever
IDE you are using.]
As a starting point for testing your implementation, use
this JUnit test program we have
provided. It does not provide testing for the boundary methods, and
might not be a complete test of the ordered set methods either.
Therefore, we strongly recommend that you add to this to do your own
testing as well. Remember that this BoundedOrderedSetList is also
a Set, so it should pass all the SetTest226 tests as well, once you
modify that file to create the appropriate type objects.
Here is a list of general implementation restrictions
and submission requirements:
- YOU MUST NOT USE ANY OF THE JAVA API COLLECTION CLASSES
SUCH AS LINKEDLIST IN YOUR IMPLEMENTATION! You may
use Strings, and reuse any code from lectures with proper citation
of course.
- You must tar/zip all source code files for the assignment into one
compressed file for submission on Blackboard. Do not submit
individual files. Make sure that you include everything we need to
compile and run your assignment, including a copy of our
BoundedOrderedSet.java and Set.java interfaces. Submit a printout of your
BoundedOrderedSetList.java source code file for detailed feedback. (Please
do not print any files we wrote.)
- Do not put any package declarations in your files.
We do not download your programs into the same directories when
grading, causing a compiler error - and we all know that a compiler
error means a 0 for the assignment.
- You may submit a separate README plain text file or use the Blackboard
assignment text box if you feel anything about your solution needs
explaining.
- Make sure you include a header comment with your name,
this course (600.226), the date, the assignment number, Blackboard
account name, and your email address in every class (file) you
write.