7 |
|
#ifndef _NullNode_hpp_ |
8 |
|
#define _NullNode_hpp_ |
9 |
|
|
10 |
+ |
#include "Node.hpp" |
11 |
+ |
|
12 |
|
template<typename Type> |
13 |
< |
class NullNode |
13 |
> |
class NullNode : public Node<Type> |
14 |
|
{ |
13 |
– |
private: |
14 |
– |
Type value; |
15 |
– |
NullNode<Type>* next; |
16 |
– |
NullNode<Type>* previous; |
17 |
– |
bool null; |
15 |
|
public: |
16 |
< |
NullNode() : next(NULL), previous(NULL), null(true) {} |
17 |
< |
NullNode(const Type& value) : value(value), next(NULL), previous(NULL), null(false) {} |
18 |
< |
~NullNode() {} |
19 |
< |
void setValue(const Type& value) { this->value = value; } |
20 |
< |
const Type& getValue(void) const { return value; } |
24 |
< |
void addNext(const Type& value); |
25 |
< |
void addPrevious(const Type& value); |
26 |
< |
void removeNext(void); |
27 |
< |
void removePrevious(void); |
28 |
< |
void setNext(NullNode<Type>* next) { this->next = next; } |
29 |
< |
void setPrevious(NullNode<Type>* previous) { this->previous = previous; } |
30 |
< |
NullNode<Type>* getNext(void) const { return next; } |
31 |
< |
NullNode<Type>* getPrevious(void) const { return next; } |
32 |
< |
bool contains(const Type& value, NullNode<Type>* end, bool forward = true); |
33 |
< |
void remove(const Type& value, NullNode<Type>* end, bool forward = true); |
16 |
> |
void setNext(NullNode<Type>* next) { this->next = dynamic_cast<Node<Type>*>(next); } |
17 |
> |
void setPrevious(NullNode<Type>* previous) { this->previous = dynamic_cast<Node<Type>*>(previous); } |
18 |
> |
bool contains(const Type& value, Node<Type>* end, bool forward); |
19 |
> |
void remove(const Type& value, Node<Type>* end, bool forward); |
20 |
> |
void accept(Visitor<Type>* visitor, Node<Type>* end, bool forward); |
21 |
|
}; |
22 |
|
|
23 |
|
template<typename Type> |
24 |
< |
void NullNode<Type>::addNext(const Type& value) |
38 |
< |
{ |
39 |
< |
NullNode<Type>* behind = next; |
40 |
< |
|
41 |
< |
next = new NullNode<Type>(value); |
42 |
< |
next->previous = this; |
43 |
< |
next->next = behind; |
44 |
< |
behind->previous = next; |
45 |
< |
} |
46 |
< |
|
47 |
< |
template<typename Type> |
48 |
< |
void NullNode<Type>::addPrevious(const Type& value) |
49 |
< |
{ |
50 |
< |
NullNode<Type>* ahead = previous; |
51 |
< |
|
52 |
< |
previous = new NullNode<Type>(value); |
53 |
< |
previous->next = this; |
54 |
< |
previous->previous = ahead; |
55 |
< |
ahead->next = previous; |
56 |
< |
} |
57 |
< |
|
58 |
< |
template<typename Type> |
59 |
< |
void NullNode<Type>::removeNext(void) |
24 |
> |
bool NullNode<Type>::contains(const Type& value, Node<Type>* end, bool forward) |
25 |
|
{ |
26 |
< |
NullNode<Type>* behind = next->next; |
62 |
< |
|
63 |
< |
delete next; |
64 |
< |
|
65 |
< |
next = behind; |
66 |
< |
next->previous = this; |
67 |
< |
} |
68 |
< |
|
69 |
< |
template<typename Type> |
70 |
< |
void NullNode<Type>::removePrevious(void) |
71 |
< |
{ |
72 |
< |
NullNode<Type>* ahead = previous->previous; |
73 |
< |
|
74 |
< |
delete previous; |
26 |
> |
Node<Type>* another = forward ? Node<Type>::next : Node<Type>::previous; |
27 |
|
|
28 |
< |
previous = ahead; |
77 |
< |
previous->next = this; |
28 |
> |
if (end == this) return false; else return another->contains(value, end, forward); |
29 |
|
} |
30 |
|
|
31 |
|
template<typename Type> |
32 |
< |
bool NullNode<Type>::contains(const Type& value, NullNode<Type>* end, bool forward) |
32 |
> |
void NullNode<Type>::remove(const Type& value, Node<Type>* end, bool forward) |
33 |
|
{ |
34 |
< |
NullNode<Type>* another = forward ? next : previous; |
34 |
> |
Node<Type>* another = forward ? Node<Type>::next : Node<Type>::previous; |
35 |
|
|
36 |
< |
if (null && end == this) |
86 |
< |
{ |
87 |
< |
return false; |
88 |
< |
} |
89 |
< |
else if (!null && value == this->value) |
90 |
< |
{ |
91 |
< |
return true; |
92 |
< |
} |
93 |
< |
else return another->contains(value, end); |
36 |
> |
if (end != this) another->remove(value, end, forward); |
37 |
|
} |
38 |
|
|
39 |
|
template<typename Type> |
40 |
< |
void NullNode<Type>::remove(const Type& value, NullNode<Type>* end, bool forward) |
40 |
> |
void NullNode<Type>::accept(Visitor<Type>* visitor, Node<Type>* end, bool forward) |
41 |
|
{ |
42 |
< |
NullNode<Type>* another = forward ? next : previous; |
42 |
> |
Node<Type>* another = forward ? Node<Type>::next : Node<Type>::previous; |
43 |
|
|
44 |
< |
if (!null && value == this->value) |
102 |
< |
{ |
103 |
< |
next->previous = previous; |
104 |
< |
previous->next = next; |
105 |
< |
|
106 |
< |
delete this; |
107 |
< |
} |
108 |
< |
else if (end != this) another->remove(value, end, forward); |
44 |
> |
if (end != this) another->accept(visitor, end, forward); |
45 |
|
} |
46 |
|
|
47 |
|
#endif // _NullNode_hpp_ |