In the first LinkedNodeCollection mini-lab, you implemented a number of basic
observer methods in the LinkedNodeCollection and DoublyLinkedNodeCollection
classes. In this lab, you will turn your focus to the add
and remove methods.
add method for a LinkedNodeCollectionThe specification for the LinkedNodeCollection.add method is that
it always adds the new element to the beginning of the list. The test cases
for the add method include:
The following diagrams illustrate the effect of the add method
for these cases.
Before add After add
Before add After add
Before add After add
Using these test cases to structure the code, we could develop the following,
somewhat verbose, version of the add method.
public boolean add(Object o)
{
if ( isEmpty() )
{
ListNode temp = new ListNode(o, null);
head = temp;
numElts = 1;
return true;
}
else if ( size() == 1 )
{
ListNode temp = new ListNode(o, head);
head = temp;
numElts = 2;
return true;
}
else if ( size() > 1 )
{
ListNode temp = new ListNode(o, head);
head = temp;
numElts++;
return true;
}
else // should never get here!
return false;
}
LinkedNodeCollectionadd method in LinkedNodeCollection
and compare it to the code developed above. Does the implementation
in the LinkedNodeCollection class address all the "extreme examples"
or boundary cases it needs to address?add method in DoublyLinkedNodeCollection.
Are they the same as for the LinkedNodeCollection class, or do
you need to add any? Develop "Before" and "After" diagrams for these
cases.add method in the DoublyLinkedNodeCollection
class.remove method include the following:
| empty list | remove 1st element (others are left) |
| element is not in list (empty list is special case) | remove element from middle of list |
| remove only element | remove last element in list |
remove method in the LinkedNodeCollection
class.clear method in the LinkedNodeCollection
class.Author: Alyce Brady abrady@kzoo.edu