Skip to main content
added 35 characters in body
Source Link
coderodde
  • 31.9k
  • 15
  • 78
  • 204

Iterator and nodes

I would decouple the linked list nodes from the iterators. As of now, in order to iterate over your list, a person must write

for (auto i = lst.begin(); i != lst.end(); i = i ->next)
{
    cout << i->key << " ";
}

which is not quite idiomatic C++. So. What you could do is to manipulate the data in terms of Node s and leave the iterators what they are supposed to do. Also, what comes to the iterators, you could declare something like

struct Node_iterator {
    Node_iterator(Node* node) : current_node(node) {}
    Node* current_node;
    
    void operator++() { current_node = current_node->next; }
    void operator--() { current_node = current_node->prev; }
    T operator*() { return current_node->key; }
    bool operator!=(Node_iterator&& other)
    {
        return current_node != other.current_node;
    }
};

Taking the above, and rewriting the list algorithms in terms of nodes and not iterators, may lead to a more idiomatic iteration construct:

for (auto i = lst2.begin(); i != lst2.end(); ++i)
{
    cout << *i << " ";
}

Iterator and nodes

I would decouple the linked list nodes from the iterators. As of now, in order to iterate over your list, a person must write

for (auto i = lst.begin(); i != lst.end(); i = i ->next)
{
    cout << i->key << " ";
}

which is not quite idiomatic C++. So. What you could do is to manipulate the data in terms of Node s and the iterators. Also, what comes to the iterators, you could declare something like

struct Node_iterator {
    Node_iterator(Node* node) : current_node(node) {}
    Node* current_node;
    
    void operator++() { current_node = current_node->next; }
    void operator--() { current_node = current_node->prev; }
    T operator*() { return current_node->key; }
    bool operator!=(Node_iterator&& other)
    {
        return current_node != other.current_node;
    }
};

Taking the above, and rewriting the list algorithms in terms of nodes and not iterators, may lead to a more idiomatic iteration construct:

for (auto i = lst2.begin(); i != lst2.end(); ++i)
{
    cout << *i << " ";
}

Iterator and nodes

I would decouple the linked list nodes from the iterators. As of now, in order to iterate over your list, a person must write

for (auto i = lst.begin(); i != lst.end(); i = i ->next)
{
    cout << i->key << " ";
}

which is not quite idiomatic C++. So. What you could do is to manipulate the data in terms of Node s and leave the iterators what they are supposed to do. Also, what comes to the iterators, you could declare something like

struct Node_iterator {
    Node_iterator(Node* node) : current_node(node) {}
    Node* current_node;
    
    void operator++() { current_node = current_node->next; }
    void operator--() { current_node = current_node->prev; }
    T operator*() { return current_node->key; }
    bool operator!=(Node_iterator&& other)
    {
        return current_node != other.current_node;
    }
};

Taking the above, and rewriting the list algorithms in terms of nodes and not iterators, may lead to a more idiomatic iteration construct:

for (auto i = lst2.begin(); i != lst2.end(); ++i)
{
    cout << *i << " ";
}
Source Link
coderodde
  • 31.9k
  • 15
  • 78
  • 204

Iterator and nodes

I would decouple the linked list nodes from the iterators. As of now, in order to iterate over your list, a person must write

for (auto i = lst.begin(); i != lst.end(); i = i ->next)
{
    cout << i->key << " ";
}

which is not quite idiomatic C++. So. What you could do is to manipulate the data in terms of Node s and the iterators. Also, what comes to the iterators, you could declare something like

struct Node_iterator {
    Node_iterator(Node* node) : current_node(node) {}
    Node* current_node;
    
    void operator++() { current_node = current_node->next; }
    void operator--() { current_node = current_node->prev; }
    T operator*() { return current_node->key; }
    bool operator!=(Node_iterator&& other)
    {
        return current_node != other.current_node;
    }
};

Taking the above, and rewriting the list algorithms in terms of nodes and not iterators, may lead to a more idiomatic iteration construct:

for (auto i = lst2.begin(); i != lst2.end(); ++i)
{
    cout << *i << " ";
}