Source of contains.cpp


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: // Returns either a pointer to the node containing a given entry
  5: // or the null pointer if the entry is not in the bag.
  6: template<class ItemType>
  7: Node<ItemType>* LinkedBag<ItemType>::
  8:                 getPointerTo(const ItemType& target) const
  9: {
 10:    bool found = false;
 11:    Node<ItemType>* curPtr = headPtr;
 12:    
 13:    while (!found && (curPtr != nullptr))
 14:    {
 15:       if (target == curPtr->getItem())
 16:          found = true;
 17:       else
 18:          curPtr = curPtr->getNext();
 19:    } // end while
 20:    
 21:    return curPtr;
 22: } // end getPointerTo

 24: template<class ItemType>
 25: bool LinkedBag<ItemType>::contains(const ItemType& anEntry) const
 26: {
 27:    return (getPointerTo(anEntry) != nullptr);
 28: }  // end contains