X305: LinkedBag removeAll

Write a LinkedBag<T> member method called removeAll() that deletes an item and any duplicates of the item from a bag, returning a boolean value indicating if anything was removed. Your removeAll() method implementation will be inserted/compiled inside the LinkedBag<T> code available to you through the example configured assignment ExLinkedBagsJUnit that you can download in Eclipse (the class name has been shortened in this question to "LinkedBag"). The author's source code is also available online.

The member fields your method implementations may access/change are:

/**
 * A class of bags whose entries are stored in a chain of linked nodes.
 * 
 * @author Frank M. Carrano
 * @author Timothy M. Henry
 * @version 4.0
 */
public final class LinkedBag<T> implements BagInterface<T>
{
    private Node firstNode;  // Reference to first node
    private int numberOfEntries;
    ...
}

Your implementation code for this problem may also access/invoke any of the Bag API methods. (Your solution code may also include helper methods.)

The removeAll() member you must write might be used like this:

LinkedBag<String> bag = new LinkedBag<String>();
System.out.println("bag.removeAll(\"a\") = " + bag.removeAll("a"));
// prints false

bag.add("a");
System.out.println("bag.removeAll(\"a\") = " + bag.removeAll("a"));
// prints true

bag.add("b");
bag.add("c");
bag.add("c");
System.out.println("bag.removeAll(\"a\") = " + bag.removeAll("a"));
// prints false

System.out.println("bag.removeAll("c") = " + bag.removeAll("c"));
// prints true

System.out.println("bag.getCurrentSize() = " + bag.getCurrentSize());
// prints 1

Write your implementation of removeAll() below.

Your Answer:

Feedback

Your feedback will appear here when you check your answer.