Source of LinkedBagExcerpts.cpp


  1: // Additions to the header file:

  3: // Fills the vector bagContents with the data in the nodes of
  4: // the linked chain to which curPtr points.
  5: void fillVector(vector<ItemType>& bagContents, Node<ItemType>* curPtr) const;

  7: // Locates a given entry within this bag.
  8: // Returns either a pointer to the node containing a given entry or
  9: // the null pointer if the entry is not in the bag.
 10: Node<ItemType>* getPointerTo(const ItemType& target,
 11:                              Node<ItemType>* curPtr) const;


 14: // Additions to the implementation file:


 17: template<class ItemType>
 18: void LinkedBag<ItemType>::fillVector(vector<ItemType>& bagContents,
 19:                                      Node<ItemType>* curPtr) const
 20: {
 21:    if (curPtr != nullptr)
 22:    {
 23:       bagContents.push_back(curPtr->getItem());
 24:       fillVector(bagContents, curPtr->getNext());
 25:    } // end if
 26: }  // end fillVector

 28: // Returns either a pointer to the node containing a given entry or
 29: // the null pointer if the entry is not in the bag.
 30: template<class ItemType>
 31: Node<ItemType>* LinkedBag<ItemType>::getPointerTo(const ItemType& target,
 32:                                                   Node<ItemType>* curPtr) const
 33: {
 34:    Node<ItemType>* result = nullptr;
 35:    if (curPtr != nullptr)
 36:    {
 37:       if (target== curPtr->getItem())
 38:          result = curPtr;
 39:       else
 40:          result = getPointerTo(target, curPtr->getNext());
 41:    } // end if
 42:    
 43:    return result;
 44: }  // end getPointerTo