| CSC 207 | Algorithms and Object Oriented Design | Spring 2011 |
Summary: In this lab, you will practice fiddling with an implementation of a LinkedList in Java by iterating over a list and removing all instances of a particular element.
weiss.nonstandard.LinkedList class
has only one member variable:
private ListNode<AnyType> header;
weiss.nonstandard.ListNode has just two member variables:
public AnyType element;
public ListNode<AnyType> next;
remove only removes the
first instance of an item in a list. It does so by first
calling findPrevious to get an iterator situated at the
node immediately before the item. (This operation requires a linear
scan of the list from the beginning.) It can then access the current
node of the iterator and make it bypass the node containing the item
it wants to delete.
In this lab, you will be directly traversing nodes in the linked list
to bypass any nodes that contain an element to be deleted.
weiss.nonstandard.LinkedList.java
/**
* Remove all occurences of the specified item.
*
* @param x the item to remove
*/
public void removeAll( AnyType x )
{
}
x. Thus, you will
need to add a variable declaration for the "current" node you are
visiting. Add this declaration to the removeAll
method.
while (CONDITION) {} skeleton with your condition
to your implementation.
Write a small driver program to test your implementation
of removeAll. Do NOT add your program to the book code
hierarchy. Instead, place it with your other files for labs you've
done as part of the course.
Be sure to test cases where the object to be found is in the beginning, middle, and end of the list, as well as appears multiple times.
/**
* Find the intersection of two sorted lists, assuming they are both
* in sorted order.
*
* @param listA Ordered List of Comparables
* @param listB Ordered List of Comparables
* @return A new ordered list containing the intersection of listA and listB
*/
public <AnyType extends <? super Comparable> static LinkedList<AnyType>
intersect( LinkedList<AnyType> listA, LinkedList<AnyType> listB );
ListUtil and add the method
declaration above to it.
LinkedList
that will be returned as the intersection in the body of the method.
iterA
and iterB, iterators for listA
and listB, respectively.
a and b,
which will be elements from iterA
and iterB, respectively.
a and b are the
current elements from each list's iterator, write down your answers to
each of the following questions.
a.compareTo(b)==0?
iterA
iterB
a.compareTo(b)<0?
iterA
iterB
a.compareTo(b)>0?
iterA
iterB
iterA.hasNext() is true?
If iterB.hasNext() is true?
intersect. You may use
either java.util.LinkedList or the weiss
implementation.
main method to test your code.