@@ -29,7 +29,7 @@ class LinkedListNode {
29
29
T _value;
30
30
public:
31
31
LinkedListNode<T>* next;
32
- LinkedListNode (const T val): _value(val), next(nullptr ) {}
32
+ LinkedListNode (T val): _value(std::move( val) ), next(nullptr ) {}
33
33
~LinkedListNode (){}
34
34
const T& value () const { return _value; };
35
35
T& value (){ return _value; }
@@ -43,11 +43,13 @@ class LinkedList {
43
43
typedef std::function<bool (const T&)> Predicate;
44
44
private:
45
45
ItemType* _root;
46
+ ItemType* _last;
46
47
OnRemove _onRemove;
47
48
48
49
class Iterator {
49
50
ItemType* _node;
50
51
ItemType* _nextNode = nullptr ;
52
+ friend class LinkedList ;
51
53
public:
52
54
Iterator (ItemType* current = nullptr ) : _node(current) {
53
55
_nextNode = _node != nullptr ? _node->next : nullptr ;
@@ -64,23 +66,43 @@ class LinkedList {
64
66
const T& operator * () const { return _node->value (); }
65
67
const T* operator -> () const { return &_node->value (); }
66
68
};
69
+
70
+ void _remove (ItemType* pit, ItemType* it) {
71
+ if (pit == nullptr ){ // item is root
72
+ _root = _root->next ;
73
+ if (_root == nullptr ) {
74
+ _last = nullptr ;
75
+ }
76
+ } else {
77
+ pit->next = it->next ;
78
+ if (it == _last) {
79
+ _last = pit;
80
+ }
81
+ }
82
+
83
+ if (_onRemove) {
84
+ _onRemove (it->value ());
85
+ }
86
+
87
+ delete it;
88
+ }
67
89
68
90
public:
69
91
typedef const Iterator ConstIterator;
70
92
ConstIterator begin () const { return ConstIterator (_root); }
71
93
ConstIterator end () const { return ConstIterator (nullptr ); }
72
94
73
- LinkedList (OnRemove onRemove) : _root(nullptr ), _onRemove(onRemove) {}
74
- ~LinkedList (){ }
75
- void add (const T& t){
76
- auto it = new ItemType (t );
95
+ LinkedList (OnRemove onRemove) : _root(nullptr ), _last( nullptr ), _onRemove(onRemove) {}
96
+ ~LinkedList () { free (); }
97
+ void add (T t){
98
+ auto it = new ItemType (std::move (t) );
77
99
if (!_root){
78
100
_root = it;
101
+ _last = it;
79
102
} else {
80
- auto i = _root;
81
- while (i->next ) i = i->next ;
82
- i->next = it;
103
+ _last->next = it;
83
104
}
105
+ _last = it;
84
106
}
85
107
T& front () const {
86
108
return _root->value ();
@@ -124,20 +146,10 @@ class LinkedList {
124
146
}
125
147
bool remove (const T& t){
126
148
auto it = _root;
127
- auto pit = _root ;
149
+ auto pit = decltype (it) { nullptr } ;
128
150
while (it){
129
151
if (it->value () == t){
130
- if (it == _root){
131
- _root = _root->next ;
132
- } else {
133
- pit->next = it->next ;
134
- }
135
-
136
- if (_onRemove) {
137
- _onRemove (it->value ());
138
- }
139
-
140
- delete it;
152
+ _remove (pit, it);
141
153
return true ;
142
154
}
143
155
pit = it;
@@ -147,26 +159,36 @@ class LinkedList {
147
159
}
148
160
bool remove_first (Predicate predicate){
149
161
auto it = _root;
150
- auto pit = _root ;
162
+ auto pit = decltype (it) { nullptr } ;
151
163
while (it){
152
164
if (predicate (it->value ())){
153
- if (it == _root){
154
- _root = _root->next ;
155
- } else {
156
- pit->next = it->next ;
157
- }
158
- if (_onRemove) {
159
- _onRemove (it->value ());
160
- }
161
- delete it;
165
+ _remove (pit, it);
162
166
return true ;
163
167
}
164
168
pit = it;
165
169
it = it->next ;
166
170
}
167
171
return false ;
168
172
}
169
-
173
+ bool remove (const ConstIterator& t, const ConstIterator& where = ConstIterator(nullptr )) {
174
+ if (where._node ) {
175
+ if ((where._nextNode ) != t._node ) return false ;
176
+ _remove (where._node , t._node );
177
+ return true ;
178
+ }
179
+
180
+ auto it = _root;
181
+ auto pit = decltype (it) { nullptr };
182
+ while (it){
183
+ if (it == t._node ){
184
+ _remove (pit, it);
185
+ return true ;
186
+ }
187
+ pit = it;
188
+ it = it->next ;
189
+ }
190
+ return false ;
191
+ }
170
192
void free (){
171
193
while (_root != nullptr ){
172
194
auto it = _root;
@@ -177,6 +199,7 @@ class LinkedList {
177
199
delete it;
178
200
}
179
201
_root = nullptr ;
202
+ _last = nullptr ;
180
203
}
181
204
};
182
205
0 commit comments