Source of ListingC7-1.h


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

  4: // Listing C7-1.

  6: #ifndef LINKED_ITERATOR_
  7: #define LINKED_ITERATOR_

  9: #include <iterator>
 10: #include "Node.h"

 12: template <class ItemType>
 13: class LinkedIterator : public std::iterator<std::input_iterator_tag, int>
 14: {
 15: private:
 16:    // ADT associated with iterator
 17:    const std::shared_ptr<LinkedList<ItemType>> containerPtr;
 18:    
 19:    // Current location in collection
 20:    std::shared_ptr<Node<ItemType>> currentItemPtr;
 21:    
 22: public:
 23:    LinkedIterator(std::shared_ptr<LinkedList<ItemType>> someList,
 24:                   std::shared_ptr<Node<ItemType>> nodePtr = nullptr);
 25:    
 26:    /** Overloaded dereferencing operator.
 27:     @return  The item at the position referenced by iterator. */
 28:    const ItemType operator*();
 29:    
 30:    /** Overloaded prefix increment operator.
 31:     @return  The iterator referencing the next position in the list. */
 32:    LinkedIterator<ItemType> operator++();
 33:    
 34:    /** Overloaded equality operator.
 35:     @param LinkedList  The iterator for comparison.
 36:     @return  True if this iterator references the same list and
 37:        the same position as rightHandSide, false otherwise. */    
 38:     bool operator==(const LinkedIterator<ItemType>& rightHandSide) const;
 39:    
 40:     /** Overloaded inequality operator.
 41:      @param LinkedList  The iterator for comparison.
 42:      @return  True if this iterator does not reference the same list and the
 43:         same position as rightHandSide, false otherwise. */
 44:    bool operator!=(const LinkedIterator<ItemType>& rightHandSide) const;
 45: }; // end LinkedIterator

 47: #include "LinkedIterator.cpp"
 48: #endif