Data Structures in C++
Iterator.hpp
<<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD Go to the documentation of this file.
1 /**
2  * @author Douglas De Rizzo Meneghetti (douglasrizzom@gmail.com)
3  * @date 2017-7-5
4  * @brief Object that provides iterative powers to classes composed of Node
5  */
6 
7 #ifndef AULA1_ITERATOR_HPP
8 #define AULA1_ITERATOR_HPP
9 
10 //! This object provides iterative powers to classes that have Node as their
11 // underlying data storage objects.
12 //! \tparam T The type of object the Node inside the Iterator will hold.
13 template<class T>
14 class Iterator {
15  private:
16 
17  Node <T> *node;
18  bool firstRun = true;
19 
20  public:
21 
22  //! Creates an iterator.
23  //! \param n The starting node for the iteration
24  explicit Iterator(Node <T> *n) {
25  node = n;
26  }
27 
28  //! Returns whether the iterator has a next value to explore.
29  //! \return True if there is a next value, otherwise false
30  bool hasNext() {
31  return firstRun and node != NULL or node->getNext() != NULL;
32  }
33 
34  //! Returns whether the iterator has a previous value to explore.
35  //! \return True if there is a previous value, otherwise false
36  bool hasPrevious() {
37  return node->getPrevious() != NULL;
38  }
39 
40  //! Go to the next value.
41  //! \return The next value in the iteration
42  T next() {
43  if (firstRun) {
44  firstRun = ! firstRun;
45  return node->getValue();
46  }
47 
48  node = node->getNext();
49  return node->getValue();
50  }
51 
52  //! Go to the previous value.
53  //! \return The previous value in the iteration
54  T previous() {
55  node = node->getNext();
56  return node->getValue();
57  }
58 
59  //! Get the current value the iterator holds.
60  //! \return The current value in the iteration
61  T current() {
62  return node->getValue();
63  }
64 };
65 
66 #endif //AULA1_ITERATOR_HPP
T next()
Go to the next value.
Definition: Iterator.hpp:42
T previous()
Go to the previous value.
Definition: Iterator.hpp:54
bool firstRun
Definition: Iterator.hpp:18
bool hasNext()
Returns whether the iterator has a next value to explore.
Definition: Iterator.hpp:30
T current()
Get the current value the iterator holds.
Definition: Iterator.hpp:61
Node< T > * node
Definition: Iterator.hpp:17
bool hasPrevious()
Returns whether the iterator has a previous value to explore.
Definition: Iterator.hpp:36
Iterator(Node< T > *n)
Creates an iterator.
Definition: Iterator.hpp:24
======= ======= >>>>>>> bded2143692dca559ffcc9e7202d9eb5fbfc45bf ======= >>>>>>> master Go to the documentation of this file.
1 /**
2  * @author Douglas De Rizzo Meneghetti (douglasrizzom@gmail.com)
3  * @date 2017-7-5
4  * @brief Object that provides iterative powers to classes composed of Node
5  */
6 
7 #ifndef AULA1_ITERATOR_HPP
8 #define AULA1_ITERATOR_HPP
9 
10 //! This object provides iterative powers to classes that have Node as their underlying data storage objects.
11 //! \tparam T The type of object the Node inside the Iterator will hold.
12 template<class T>
13 class Iterator {
14  private:
15 
16  Node <T> *node;
17  bool firstRun = true;
18 
19  public:
20 
21  //! Creates an iterator.
22  //! \param n The starting node for the iteration
23  explicit Iterator(Node <T> *n) {
24  node = n;
25  }
26 
27  //! Returns whether the iterator has a next value to explore.
28  //! \return True if there is a next value, otherwise false
29  bool hasNext() {
30  return firstRun and node != NULL or node->getNext() != NULL;
31  }
32 
33  //! Returns whether the iterator has a previous value to explore.
34  //! \return True if there is a previous value, otherwise false
35  bool hasPrevious() {
36  return node->getPrevious() != NULL;
37  }
38 
39  //! Go to the next value.
40  //! \return The next value in the iteration
41  T next() {
42  if (firstRun) {
43  firstRun = ! firstRun;
44  return node->getValue();
45  }
46 
47  node = node->getNext();
48  return node->getValue();
49  }
50 
51  //! Go to the previous value.
52  //! \return The previous value in the iteration
53  T previous() {
54  node = node->getNext();
55  return node->getValue();
56  }
57 
58  //! Get the current value the iterator holds.
59  //! \return The current value in the iteration
60  T current() {
61  return node->getValue();
62  }
63 };
64 
65 #endif //AULA1_ITERATOR_HPP
Node used inside other data structures.
Definition: Node.hpp:15
T next()
Go to the next value.
Definition: Iterator.hpp:41
T previous()
Go to the previous value.
Definition: Iterator.hpp:53
bool firstRun
Definition: Iterator.hpp:17
bool hasNext()
Returns whether the iterator has a next value to explore.
Definition: Iterator.hpp:29
T current()
Get the current value the iterator holds.
Definition: Iterator.hpp:60
Object that provides iterative powers to classes composed of Node.
Definition: Iterator.hpp:13
Node< T > * node
Definition: Iterator.hpp:16
bool hasPrevious()
Returns whether the iterator has a previous value to explore.
Definition: Iterator.hpp:35
Iterator(Node< T > *n)
Creates an iterator.
Definition: Iterator.hpp:23
<<<<<<< HEAD <<<<<<< HEAD >>>>>>> 36f9b37... fixed dependency of ProtectedLinkedList to Iterator ======= >>>>>>> bded2143692dca559ffcc9e7202d9eb5fbfc45bf ======= >>>>>>> master