7 |
|
#ifndef _NullNode_hpp_ |
8 |
|
#define _NullNode_hpp_ |
9 |
|
|
10 |
< |
#include "Node.hpp" |
10 |
> |
#include "Visitor.hpp" |
11 |
|
|
12 |
< |
template <typename Type> |
13 |
< |
class NullNode : public Node<Type> |
12 |
> |
template<typename Type> |
13 |
> |
class NullNode |
14 |
|
{ |
15 |
|
private: |
16 |
< |
bool null; |
16 |
> |
Type value; |
17 |
|
NullNode<Type>* next; |
18 |
|
NullNode<Type>* previous; |
19 |
+ |
bool null; |
20 |
|
public: |
21 |
< |
NullNode() : null(false) {} |
22 |
< |
NullNode(const Type& value) : Node<Type>::Node(value), null(false) {} |
23 |
< |
~NullNode() {} |
21 |
> |
NullNode() : null(true) {} |
22 |
> |
NullNode(const Type& value) : value(value), null(false) {} |
23 |
> |
void setValue(const Type& value) { this->value = value; } |
24 |
> |
const Type& getValue(void) const { return value; } |
25 |
> |
void addNext(const Type& value); |
26 |
> |
void addPrevious(const Type& value); |
27 |
> |
void removeNext(void); |
28 |
> |
void removePrevious(void); |
29 |
> |
void setNext(NullNode<Type>* next) { this->next = next; } |
30 |
> |
void setPrevious(NullNode<Type>* previous) { this->previous = previous; } |
31 |
> |
NullNode<Type>* getNext(void) const { return next; } |
32 |
> |
NullNode<Type>* getPrevious(void) const { return next; } |
33 |
> |
bool contains(const Type& value, NullNode<Type>* end, bool forward = true); |
34 |
> |
void remove(const Type& value, NullNode<Type>* end, bool forward = true); |
35 |
> |
void accept(Visitor<Type>* visitor, NullNode<Type>* end, bool forward = true); |
36 |
|
}; |
37 |
|
|
38 |
+ |
template<typename Type> |
39 |
+ |
void NullNode<Type>::addNext(const Type& value) |
40 |
+ |
{ |
41 |
+ |
NullNode<Type>* behind = next; |
42 |
+ |
|
43 |
+ |
next = new NullNode<Type>(value); |
44 |
+ |
next->previous = this; |
45 |
+ |
next->next = behind; |
46 |
+ |
behind->previous = next; |
47 |
+ |
} |
48 |
+ |
|
49 |
+ |
template<typename Type> |
50 |
+ |
void NullNode<Type>::addPrevious(const Type& value) |
51 |
+ |
{ |
52 |
+ |
NullNode<Type>* ahead = previous; |
53 |
+ |
|
54 |
+ |
previous = new NullNode<Type>(value); |
55 |
+ |
previous->next = this; |
56 |
+ |
previous->previous = ahead; |
57 |
+ |
ahead->next = previous; |
58 |
+ |
} |
59 |
+ |
|
60 |
+ |
template<typename Type> |
61 |
+ |
void NullNode<Type>::removeNext(void) |
62 |
+ |
{ |
63 |
+ |
NullNode<Type>* behind = next->next; |
64 |
+ |
|
65 |
+ |
delete next; |
66 |
+ |
|
67 |
+ |
next = behind; |
68 |
+ |
next->previous = this; |
69 |
+ |
} |
70 |
+ |
|
71 |
+ |
template<typename Type> |
72 |
+ |
void NullNode<Type>::removePrevious(void) |
73 |
+ |
{ |
74 |
+ |
NullNode<Type>* ahead = previous->previous; |
75 |
+ |
|
76 |
+ |
delete previous; |
77 |
+ |
|
78 |
+ |
previous = ahead; |
79 |
+ |
previous->next = this; |
80 |
+ |
} |
81 |
+ |
|
82 |
+ |
template<typename Type> |
83 |
+ |
bool NullNode<Type>::contains(const Type& value, NullNode<Type>* end, bool forward) |
84 |
+ |
{ |
85 |
+ |
NullNode<Type>* another = forward ? next : previous; |
86 |
+ |
|
87 |
+ |
if (null && end == this) |
88 |
+ |
{ |
89 |
+ |
return false; |
90 |
+ |
} |
91 |
+ |
else if (!null && value == this->value) |
92 |
+ |
{ |
93 |
+ |
return true; |
94 |
+ |
} |
95 |
+ |
else return another->contains(value, end); |
96 |
+ |
} |
97 |
+ |
|
98 |
+ |
template<typename Type> |
99 |
+ |
void NullNode<Type>::remove(const Type& value, NullNode<Type>* end, bool forward) |
100 |
+ |
{ |
101 |
+ |
NullNode<Type>* another = forward ? next : previous; |
102 |
+ |
|
103 |
+ |
if (!null && value == this->value) |
104 |
+ |
{ |
105 |
+ |
next->previous = previous; |
106 |
+ |
previous->next = next; |
107 |
+ |
|
108 |
+ |
delete this; |
109 |
+ |
} |
110 |
+ |
else if (end != this) another->remove(value, end, forward); |
111 |
+ |
} |
112 |
+ |
|
113 |
+ |
template<typename Type> |
114 |
+ |
void NullNode<Type>::accept(Visitor<Type>* visitor, NullNode<Type>* end, bool forward) |
115 |
+ |
{ |
116 |
+ |
NullNode<Type>* another = forward ? next : previous; |
117 |
+ |
|
118 |
+ |
if (!null) visitor->visit(value); |
119 |
+ |
|
120 |
+ |
if (end != this) another->accept(visitor, end, forward); |
121 |
+ |
} |
122 |
+ |
|
123 |
|
#endif // _NullNode_hpp_ |