Skip to content

Commit 1f27685

Browse files
committed
LinkedList: Simplify iterator
There's no real need for holding two members; for strictly safe incrementing, we're going to have to branch regardless.
1 parent a68a18c commit 1f27685

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

src/StringArray.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,11 @@ class LinkedList {
4848

4949
class Iterator {
5050
ItemType* _node;
51-
ItemType* _nextNode = nullptr;
5251
friend class LinkedList;
5352
public:
54-
Iterator(ItemType* current = nullptr) : _node(current) {
55-
_nextNode = _node != nullptr ? _node->next : nullptr;
56-
}
57-
Iterator(const Iterator& i) : _node(i._node) {
58-
_nextNode = _node != nullptr ? _node->next : nullptr;
59-
}
53+
Iterator(ItemType* current = nullptr) : _node(current) {};
6054
Iterator& operator ++() {
61-
_node = _nextNode;
62-
_nextNode = _node != nullptr ? _node->next : nullptr;
55+
if (_node) _node = _node->next;
6356
return *this;
6457
}
6558
bool operator != (const Iterator& i) const { return _node != i._node; }
@@ -172,7 +165,7 @@ class LinkedList {
172165
}
173166
bool remove(const ConstIterator& t, const ConstIterator& where = ConstIterator(nullptr)) {
174167
if (where._node) {
175-
if ((where._nextNode) != t._node) return false;
168+
if ((where._node->next) != t._node) return false;
176169
_remove(where._node, t._node);
177170
return true;
178171
}

0 commit comments

Comments
 (0)