public final class LinkedBag /* implements BagInterface //this should be here, but not all of the interface will be implemented in class */ { private Node firstNode; // Reference to first node private int numberOfEntries; public LinkedBag() { firstNode = null; numberOfEntries = 0; } // end default constructor /** Adds a new entry to this bag. @param newEntry The object to be added as a new entry @return True if the addition is successful, or false if not. */ public boolean add(T newEntry) // OutOfMemoryError possible { // Add to beginning of chain: Node newNode = new Node(newEntry); newNode.next = firstNode; // Make new node reference rest of chain // (firstNode is null if chain is empty) firstNode = newNode; // New node is at beginning of chain numberOfEntries++; return true; } // end add /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if the bag contains anEntry, or false otherwise. */ public boolean contains(T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.data)) found = true; else currentNode = currentNode.next; } // end while return found; } // end contains /** Removes one occurrence of a given entry from this bag, if possible. @param anEntry The entry to be removed. @return True if the removal was successful, or false otherwise. */ public boolean remove(T anEntry) { } // end remove private class Node { private T data; // Entry in bag private Node next; // Link to next node private Node(T dataPortion) { this(dataPortion, null); } // end constructor private Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } // end constructor } // end Node } // end LinkedBag