Source of 3.23.java


  1:          // Locates a given entry within this bag.
  2:         // Returns a reference to the node containing the entry, if located,
  3:         // or null otherwise.
  4:         private Node getReferenceTo(T anEntry)
  5:         {
  6:                 boolean found = false;
  7:                 Node currentNode = firstNode;
  8:                 
  9:                 while (!found && (currentNode != null))
 10:                 {
 11:                         if (anEntry.equals(currentNode.data))
 12:                                 found = true;
 13:                         else
 14:                                 currentNode = currentNode.next;
 15:                 } // end while
 16:      
 17:                 return currentNode;
 18:         } // end getReferenceTo
 19: 
 20: 
 21:         /** Removes one occurrence of a given entry from this bag, if possible.
 22:        @param anEntry  The entry to be removed.
 23:        @return  True if the removal was successful, or false otherwise. */
 24:    public boolean remove(T anEntry) 
 25:         {
 26:                 boolean result = false;
 27:       Node nodeN = getReferenceTo(anEntry);
 28:       
 29:       if (nodeN != null)
 30:       {
 31:          nodeN.data = firstNode.data; // Replace located entry with entry in first node
 32:          firstNode = firstNode.next;  // Remove first node
 33:          numberOfEntries--;
 34:          result = true;
 35:       } // end if
 36:          
 37:                 return result;
 38:         } // end remove