Make LinkedList iterator safe against removal of cur item

This commit is contained in:
Davide Depau 2021-03-07 14:21:56 +01:00
parent 1d46269ced
commit 26563e69bc
1 changed files with 12 additions and 3 deletions

View File

@ -47,10 +47,19 @@ class LinkedList {
class Iterator {
ItemType* _node;
ItemType* _nextNode = nullptr;
public:
Iterator(ItemType* current = nullptr) : _node(current) {}
Iterator(const Iterator& i) : _node(i._node) {}
Iterator& operator ++() { _node = _node->next; return *this; }
Iterator(ItemType* current = nullptr) : _node(current) {
_nextNode = _node != nullptr ? _node->next : nullptr;
}
Iterator(const Iterator& i) : _node(i._node) {
_nextNode = _node != nullptr ? _node->next : nullptr;
}
Iterator& operator ++() {
_node = _nextNode;
_nextNode = _node != nullptr ? _node->next : nullptr;
return *this;
}
bool operator != (const Iterator& i) const { return _node != i._node; }
const T& operator * () const { return _node->value(); }
const T* operator -> () const { return &_node->value(); }