| CSC 207 | Algorithms and Object Oriented Design | Spring 2009 |
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 occurence of an item.
* @param x the item to remove
*/
public void removeAll( AnyType x )
{
}
x. Use a while loop for this
purpose. Thus, you will need to declare the "current" node you are
visiting. Add a variable declaration for such a node to your
implementation.
while (CONDITION) {} skeleton with your condition
to your implementation.
removeAll. Be sure to test cases where the object to
be found is in the beginning, middle, and end of the list.
/**
* Find the intersection of two sorted lists.
*
* @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 );
LinkedList
that will be returned as the intersection.
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 in its own class file. You may
use either tjava.util.LinkedList or
the weiss implementation.
main method to test your code.