Gettysburg College

CS 216
Data Structures

Spring 2024

Assignment 3

Due: Mon, Feb 12, by 11:59pm
  • method stubs: DLinkedList.java
  • junit tests: DLInkedListTest.java
  • coverage.pdf
  • api.pdf
Due: Thu, Feb 15, 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

:: NOT allowed to call any of the DLinkedList methods from within any other method.
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.

Contact the instructor immediately if you could not get either of these to work.

E removeFirst()

Removes the first element in the list.

JUnit: _?_ cases

E removeLast()

Removes the last element in the list.

JUnit: _?_ cases

E remove(int index)

Removes the element at the given index.

JUnit: 10 cases

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: 10+ cases. 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).
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 need to include a JUnit test for this method. It will be tested implicitly in JUnit by testing the method containsIter(...)

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.

The should be no Node code anywhere in this method.

JUnit: 7 cases, same as contains

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

JUnit: No JUnit test cases are needed. This is a method of the inner iterator class and will be tested implicitly in JUnit by testing the method removeAllIter(...).

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 Node code anywhere in this method.

JUnit: same as removeAll

@Test
void test_iterFails()

Add this method in the Tester class. For each type of list (empty, single, multi), it tests that list's iterator 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.

JUnit: To test you could write:

list = load( m,y,n,u,m,b,e,r,s);
Iterator<???> iter = list.iterator();
assertTrue(iter.next(), ...) or iter.remove() [sometimes in try/catch]
assert toStingNext/Prev for the list
boolean equals(Object list)

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 Node code 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.)