Gettysburg College

CS 216
Data Structures and Algorithms

Fall 2024

Assignment 3

Due: Mon, Sep 16, by 11:59pm
  • method stubs: DLinkedList.java
  • junit tests: DLInkedListTest.java
  • coverage.pdf
  • api.pdf
Due: Thu, Sep 19, by 11:59pm
  • DLinkedList.java
  • DLinkedListTest.java
  • coverage.pdf
This is a continuation of Assignment 2. Add the methods given below to the DLinkedList class. In addition make sure that:

Here are the examples from class:

SLinkedList.java
SLinkedListTest.java

video on addLast( 3 ) in multi
video on addLast( 3 ) in single
video on addLast( 3 ) in empty

Do not write additional methods that are not specified in the assignment and do not call any of the DLinkedList methods from within any other method (it is fine to call isEmpty but do not overuse it).
void addFirst  or   void addLast

From Assignment 2 you only need one of these methods to be correct. Adjust method load() accordingly (read comments in Tester).

Method addLast is easier and requires only 4 lines. If there were issue with both of these, make sure the issues are resolved by Monday in office hours.

E removeFirst()

Removes the first element in the list.
E removeLast()

Removes the last element in the list.
E remove(int index)

Removes the element at the given index.
boolean removeAll(E item)

Removes all occurrences of the given item in the list; do not use remove(E item) (why?), manipulate the pointers explicitly.

JUnit: Here it would be good to try different multi-lists list (e.g. list with sections of adjacent identical elements in the right places, list with distinct elements).

Iterator<E> iterator()

Returns an instance of DListIterator (see below).

Do not write JUnit tests for this method. It will be tested implicitly by other methods.

DListIterator class

Inner class for creating iterator objects that supports all operations in the Iterator interface.

Hint: Only one pointer as data member is needed to implement the iterator.

Start by implementing only the iterator methods next() and hasNext(). Later implement remove().

Do not write JUnit tests for the methods of this class. They will be tested implicitly by other methods.

Class examples: Stack.java, IteratorTest.java

boolean containsIter(E item)

Same as contains but uses an iterator over this list to visit the nodes as it searches for the item.

Use enhanced for-loop. The should be no mention of Node anywhere in this method.

void remove()

Implement the method of the DListIterator class. (see Iterator). Note that before each call to remove() there must have been a call to next(). In other words, the iterator does not allow:
  • successive calls of remove()
  • a call of remove() as soon as the Iterator is constructed
In each case IllegalStateException is thrown. You will need to add a boolean data member to the iterator class, see Stack.java, IteratorTest.java

Do not write JUnit tests for this method. It will be tested implicitly by other method.

boolean removeAllIter(E item)

Same as removeAll but uses an iterator over this list to visit the nodes and remove them.

This method must use while loop, since .remove() of iterators cannot be called inside enhanced for loop.

The should be no mention of Node anywhere in this method.

@Test
void test_iterFails()

Add this method in the Tester class. For each type of list (empty, single, multi) it tests the iterator of that list for all possible failing conditions of the methods next() and remove(), i.e. the conditions under which these methods throw exceptions.

The goal is to have each line in the Iterator class marked green.

Here is the structure of the tests: one group per list, for a total of 3 groups, each group is longer than 4 lines:

list = load( m,y,n,u,m,b,e,r,s);
Iterator<???> iter = list.iterator();
    // rule-of-4 does not apply, ok (necessary) to have multiples of this line here:
    assertTrue(iter.next(), ...) or iter.remove() [sometimes in assertThrows]
assert toStingNext/Prev for the list
boolean equals(Object other)

Determines if this list is equal to the given list (i.e. have the same contents).

Implement this using two iterators (one for this list and one for the given list) and a while loop. The should be no mention of Node anywhere in this method.

Here is an example of the typical steps in implementing the method equals: Cat.java. Note that a typecast is required to convert from Object to the specific class.

JUnit: Compare the lists with themselves, with each other, and with things that are not lists. Note, that these tests are not likely to be sufficient.


What to turn in

Upload the .java and .pdf files in the Moodle dropbox. (Do NOT upload .html files.)