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