Skip to content

Instantly share code, notes, and snippets.

@BogdanAriton
Created June 15, 2021 15:29
Show Gist options
  • Save BogdanAriton/97be597f3381b7e7f24ad7a46121a4a6 to your computer and use it in GitHub Desktop.
Save BogdanAriton/97be597f3381b7e7f24ad7a46121a4a6 to your computer and use it in GitHub Desktop.
struct Iterator
{
// constructor that takes in a pointer from the linked list
Iterator() noexcept : current_node(nullptr){};
Iterator(const node_ptr &node) noexcept : current_node(node.get()){};
// incrementing means going through the list
Iterator &operator++() noexcept
{
if (current_node != nullptr)
{
previous_node = current_node;
current_node = current_node->next.get();
}
return *this;
};
// post fixing is bad in general but it has it's usages
Iterator operator++(int) noexcept
{
Iterator tempIter = *this; // we make a copy of the iterator
++*this; // we increment
return tempIter; // we return the copy before increment
};
// we need to be able to compare nodes
bool operator!=(const Iterator &other) const noexcept
{
return this->current_node != other.current_node;
};
// return the data from the node (dereference operator)
Data operator*() const noexcept
{
return this->current_node->data;
};
private:
const Node *previous_node = nullptr;
const Node *current_node = nullptr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment