Source of LinkedBag.h


  1: //  Created by Frank M. Carrano and Tim Henry.
  2: //  Copyright (c) 2013 __Pearson Education__. All rights reserved.

  4: /** ADT bag: Link-based implementation.
  5:     @file LinkedBag.h 
  6:     Listing 4-3 */
  7: #ifndef _LINKED_BAG
  8: #define _LINKED_BAG

 10: #include "BagInterface.h"
 11: #include "Node.h"

 13: template<class ItemType>
 14: class LinkedBag : public BagInterface<ItemType>
 15: {
 16: private:
 17:    Node<ItemType>* headPtr; // Pointer to first node
 18:    int itemCount;           // Current count of bag items
 19:    
 20:    // Returns either a pointer to the node containing a given entry
 21:    // or the null pointer if the entry is not in the bag.
 22:    Node<ItemType>* getPointerTo(const ItemType& target) const;
 23:    
 24: public:
 25:    LinkedBag();
 26:    LinkedBag(const LinkedBag<ItemType>& aBag); // Copy constructor
 27:    virtual ~LinkedBag();                       // Destructor should be virtual
 28:    int getCurrentSize() const;
 29:    bool isEmpty() const;
 30:    bool add(const ItemType& newEntry);
 31:    bool remove(const ItemType& anEntry);
 32:    void clear();
 33:    bool contains(const ItemType& anEntry) const;
 34:    int getFrequencyOf(const ItemType& anEntry) const;
 35:    vector<ItemType> toVector() const;
 36: }; // end LinkedBag

 38: #include "LinkedBag.cpp"
 39: #endif