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

  1. [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?
  2. [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)?
  3. [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 */
    
  4. 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.
    1. [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)
      
    2. [1] What is the running time of your method if L and S are implemented with Java ArrayLists?
    3. [1] What is the running time of your method if L and S are implemented with Java LinkedLists?
    4. [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:

[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: