Source of 3.23.java


  1: // @author Frank M. Carrano, Timothy M. Henry
  2: // @version 5.0
  3:          // Locates a given entry within this bag.
  4:         // Returns a reference to the node containing the entry, if located,
  5:         // or null otherwise.
  6:         private Node getReferenceTo(T anEntry)
  7:         {
  8:                 boolean found = false;
  9:                 Node currentNode = firstNode;
 10:                 
 11:                 while (!found && (currentNode != null))
 12:                 {
 13:                         if (anEntry.equals(currentNode.data))
 14:                                 found = true;
 15:                         else
 16:                                 currentNode = currentNode.next;
 17:                 } // end while
 18:      
 19:                 return currentNode;
 20:         } // end getReferenceTo


 23:         /** Removes one occurrence of a given entry from this bag, if possible.
 24:        @param anEntry  The entry to be removed.
 25:        @return  True if the removal was successful, or false otherwise. */
 26:    public boolean remove(T anEntry) 
 27:         {
 28:                 boolean result = false;
 29:       Node nodeN = getReferenceTo(anEntry);
 30:       
 31:       if (nodeN != null)
 32:       {
 33:          nodeN.data = firstNode.data; // Replace located entry with entry in first node
 34:          firstNode = firstNode.next;  // Remove first node
 35:          numberOfEntries--;
 36:          result = true;
 37:       } // end if
 38:          
 39:                 return result;
 40:         } // end remove