7 |
|
#ifndef _Iterator_hpp_ |
8 |
|
#define _Iterator_hpp_ |
9 |
|
|
10 |
+ |
#include "NullNode.hpp" |
11 |
+ |
|
12 |
+ |
template<typename Type> |
13 |
+ |
class DoublyLinkedList; |
14 |
+ |
|
15 |
|
template<typename Type> |
16 |
|
class Iterator |
17 |
|
{ |
18 |
|
private: |
19 |
< |
// |
19 |
> |
NullNode<Type>* front; |
20 |
> |
NullNode<Type>* back; |
21 |
> |
NullNode<Type>* here; |
22 |
> |
Iterator(NullNode<Type>* front, NullNode<Type>* back, bool forward = true) : front(front), back(back) { here = forward ? front->getNext() : back->getPrevious(); } |
23 |
|
public: |
24 |
< |
Iterator() {} |
25 |
< |
~Iterator() {} |
26 |
< |
const Iterator<Type>& operator++(); |
24 |
> |
bool next(void) { return here->getNext() != back; } |
25 |
> |
bool previous(void) { return here->getPrevious() != front; } |
26 |
> |
Iterator<Type>& operator++() { here = here->getNext(); return *this; } |
27 |
|
Iterator<Type> operator++(int); |
28 |
< |
const Iterator<Type>& operator--(); |
28 |
> |
Iterator<Type>& operator--() { here = here->getPrevious(); return *this; } |
29 |
|
Iterator<Type> operator--(int); |
30 |
+ |
const Type& operator*() { return here->getValue(); } |
31 |
+ |
// friends: |
32 |
+ |
friend class DoublyLinkedList<Type>; |
33 |
|
}; |
34 |
|
|
35 |
+ |
template<typename Type> |
36 |
+ |
Iterator<Type> Iterator<Type>::operator++(int) |
37 |
+ |
{ |
38 |
+ |
Iterator itor(*this); |
39 |
+ |
|
40 |
+ |
++*this; |
41 |
+ |
|
42 |
+ |
return itor; |
43 |
+ |
} |
44 |
+ |
|
45 |
+ |
template<typename Type> |
46 |
+ |
Iterator<Type> Iterator<Type>::operator--(int) |
47 |
+ |
{ |
48 |
+ |
Iterator itor(*this); |
49 |
+ |
|
50 |
+ |
--*this; |
51 |
+ |
|
52 |
+ |
return itor; |
53 |
+ |
} |
54 |
+ |
|
55 |
|
#endif // _Iterator_hpp_ |