Gettysburg College

CS 216
Data Structures

Spring 2024

Assignment 2

Due: Mon, Feb 5, by 11:59pm
  • method stubs: DLinkedList.java
  • junit tests: DLInkedListTest.java
  • coverage.pdf
  • api.pdf
Due: Thu, Feb 8, by 11:59pm
  • DLinkedList.java
  • DLinkedListTest.java
  • coverage.pdf

Preliminaries

Here is the minimum expectation for Monday for classes DLinkedList and DLinkedListTest (separately prepare and submit coverage and api documents, coverage.pdf and api.pdf):

Handouts

In class we implemented several methods of a (non-headed) singly-linked list, i.e. each node had a pointer only to its successor node:

SLinkedList.java (class example: non-headed, i.e. no dummy node)
SLinkedListTest.java (class example: JUnit tester)

Here is a collection of videos that demonstrate how a method is executed and how the code can be debugged:

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

Here is a short video on how to set up and run a JUnit class and Javadoc (saving to PDF was via the main menu "File->Print..."):

JUnit + Javadoc video

Description (Part I)

For this assignment you are asked to implement a Headed Doubly-Linked List:

The dummy node is a special node that you put as the programmer to make things easier for you. The user does not know about the existence of the dummy node. The data of the dummy node is null and the head always points to the dummy node.

Here is an illustration of the class example with and without a dummy node. We chose to implement the version without dummy node:

empty list
head-->▮
head-->null|*-->▮
one element
head--> 5 |*-->▮
head-->null|*--> 5 |*-->▮
multi element
head--> 8 |*--> 5 |*--> 4 |*--> 7 |*-->▮
head-->null|*--> 8 |*--> 5 |*--> 4 |*--> 7 |*-->▮

Description (Part II)

Create a class DLinkedList (in a project also called DLinkedList in your cs216 folder) such that: The DLinkedList should support the following methods of the List interface:

NOT allowed to call any of the DLinkedList methods from within any other method.
boolean isEmpty()

Determines if the list is empty.
void addFirst(E item)

Adds the given item to the front of the list.
void addLast(E item)

Adds the given item to the end of the list.
E getFirst()

Returns the first element in the list.
E getLast()

Returns the last element in the list.
boolean contains(E item)

Determines if the list contains the given item.
E get(int index)

Returns the item at the given index in the list.
E set(int index, E item)

Replaces the item in the node at the given index with the given item returns the previous item at that index.
void add(int index, E item)

Adds the given item at the given index in the list.

In other words, after this operation, the given item will be in position index in the list, so if the list currently has 5 elements, add(0, ...) has the effect of adding to the beginning and add(5, ....) has the effect of adding to the end; indices outside the range [0..5] are invalid.

Note that the dummy node is not counted, since it does not exist from the user's point of view.

void clear()

Clears all the elements in the list. (Go through the list and set the two links of each node to null.)
String toStringNext()

Returns a string representation of the list in this format: [] (for empty list), [1 2 3 4] (for non-empty list).
String toStringPrev()

Returns a string representation of the list (as in toStringNext).

Important:

  • this method must traverse the list starting from the end by following the back-pointing links
  • this method must returns exactly the same string as method toStringNext; i.e. even though we walk backwards the final string still has the items in front-to-back order (hint: s=s+thing vs s=thing+s)
  • there should be no .next anywhere in this method
In the JUnit tester you must use assertEquals with both toStringNext and toStringPrev -- this will help to ensure that the forward and back links are connected correctly.


What to turn in

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