|
5 | 5 |
|
6 | 6 | template <class T> |
7 | 7 | class DSDoublyLL{ |
| 8 | +private: |
| 9 | + /** |
| 10 | + * @brief The iterator struct - iterator for the vector class |
| 11 | + */ |
| 12 | + struct iterator{ |
| 13 | + public: |
| 14 | + /** |
| 15 | + * @brief iterator - constructor |
| 16 | + * @param ptr - pointer to start iteration from |
| 17 | + */ |
| 18 | + iterator(DSNode<T>* ptr): ptr(ptr){} |
| 19 | + |
| 20 | + /** |
| 21 | + * @brief operator * - dereferences pointer |
| 22 | + * @return - reference of the class T at the pointer's location |
| 23 | + */ |
| 24 | + T& operator*(){ |
| 25 | + return ptr->data; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @brief operator ++ - iterates to next pointer |
| 30 | + * @return this iterator after iteration |
| 31 | + */ |
| 32 | + iterator operator++(){ |
| 33 | + ptr = ptr->next; |
| 34 | + return *this; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @brief operator ++ |
| 39 | + * @return a reference to an iterator before iteration |
| 40 | + */ |
| 41 | + iterator operator++(int){ |
| 42 | + iterator tmp = *this; |
| 43 | + ptr = ptr->next; |
| 44 | + return tmp; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @brief operator == compares if two iterators are equal |
| 49 | + * @param a - first iterator to compare |
| 50 | + * @param b - second iterator to compare |
| 51 | + * @return true if the two iterators are equal |
| 52 | + */ |
| 53 | + friend bool operator==(const iterator& a, const iterator& b){ |
| 54 | + return a.ptr == b.ptr; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @brief operator != compares if two iterators are equal |
| 59 | + * @param a - first iterator to compare |
| 60 | + * @param b - second iterator to compare |
| 61 | + * @return true if the two iterators are not equal |
| 62 | + */ |
| 63 | + friend bool operator!=(const iterator& a, const iterator& b){ |
| 64 | + return a.ptr != b.ptr; |
| 65 | + } |
| 66 | + private: |
| 67 | + DSNode<T>* ptr; |
| 68 | + }; |
8 | 69 | public: |
9 | 70 | /** |
10 | 71 | * @brief DSDoublyLL - default constructor |
@@ -119,6 +180,14 @@ class DSDoublyLL{ |
119 | 180 | */ |
120 | 181 | ~DSDoublyLL(); |
121 | 182 |
|
| 183 | + inline iterator begin(){ |
| 184 | + return iterator(head); |
| 185 | + }; |
| 186 | + |
| 187 | + inline iterator end(){ |
| 188 | + return iterator(nullptr); |
| 189 | + } |
| 190 | + |
122 | 191 | private: |
123 | 192 | DSNode<T>* head = nullptr; |
124 | 193 | DSNode<T>* tail = nullptr; |
|
0 commit comments