Due: Mon, Sep 9, by 11:59pm
|
Due: Thu, Sep 12, by 11:59pm
|
DLinkedList
and DLinkedListTest
(separately prepare and submit coverage and api documents, coverage.pdf and api.pdf):
DLinkedList
:
return;
OR return false;
OR return null;
as appropriate so that the code compiles without errors; here is a sample SLinkedList.java (Monday)@param
, @return
, @throws
where appropriate (see the class examples below)DLinkedListTest
:
list = load( the-list-values ); assertTrue( list.methodToTest() ); assertEquals( list.toStringNext(), "[the expected values]" ); assertEquals( list.toStringPrev(), "[same values as above]" ); |
list = load( the-list-values ); assertTrue( list.methodToTest().equals( some-value ) ); assertEquals( list.toStringNext(), "[the expected values]" ); assertEquals( list.toStringPrev(), "[same values as above]" ); |
list = load( the-list-values ); list.methodToTest(); assertEquals( list.toStringNext(), "[the expected values]" ); assertEquals( list.toStringPrev(), "[same values as above]" ); |
list = load( the-list-values ); assertThrows( ExpectedException.class, () -> list.methodToTest() ); assertEquals( list.toStringNext(), "[the expected values]" ); assertEquals( list.toStringPrev(), "[same values as above]" ); |
get(..)
, set(..)
, contains(..)
)test_isEmpty
); void methods (see test_addLast
); and methods that return an object or throw exception (see getFirst
)head
arrow always points to a secret node called a dummy node; it is created with null
for data
data
of the dummy node is null
and the head
always points to the dummy node.
Here is an illustration of the representation 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 |*-->▮
DLinkedList
(in a project also called DLinkedList
in your cs216 folder) such that:
DLinkedList
has pointers (i.e. has data members) to both the head
of the list (the dummy node) and the tail
of the list (the last node); initially both the head
and the tail
point to the dummy nodeDLinkedList
does not keep track of its current sizeDLinkedList
methods throw exceptions where appropriate (either NoSuchElementException or IndexOutOfBoundsException, only one type of exception per method)DLinkedList
methods are documented using the Javadoc
style (see the methods getFirst and addFirst in SLinkedList.java)DLinkedList
methods are organized and tested rigorously using the JUnit framework as shown in SLinkedListTest.javaDLinkedList
should support the following methods of the List
interface:
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).
|
default constructor
Creates an empty list.
|
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.
Do not use methods addFirst and addLast .
|
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:
assertEquals with both toStringNext and toStringPrev -- this will help to ensure that the forward and back links are connected correctly.
|