C++自己的迭代器

JAt*_*dar 2 c++ iterator linked-list

我的c ++代码有点问题.我有链表(下图),我需要为自己的(学校工作)制作一个迭代器..

列表http://www.attanon.eu/list.png

我在list列表变量中包含head,last和actual节点.

我的类迭代器就是这个

class iterator
{
    Node* _node;
public:
    iterator(Node* node) : _node(node){}
    ~iterator(){ _node = nullptr; }

    iterator& operator=(const iterator& other)
    {
        _node = other._node;
        return *this;
    }
    bool operator==(const iterator& other)
    {
        if (_node == nullptr || other._node == nullptr)
        {
            return false;
        }
        else
        {
            return _node->_data == other._node->_data;
        }
    }
    bool operator!=(const iterator& other)
    {
        if (_node == nullptr || other._node == nullptr)
        {
            return false;
        }
        else
        {
            return _node->_data != other._node->_data;
        }
    }

    iterator& operator++() // prefix
    {
        if (_node != nullptr)
        {
            _node = _node->_next;
        }
        return *this;
    }
    iterator operator++(int) // postfix
    {
        iterator temp(*this);
        ++(*this);
        return temp;
    }
    T& operator*() // dereference
    {
        return _node->_data;
    }
    T* operator->() // šipková notace
    {
        return &*(List<T>::iterator)*this;
    }
};
Run Code Online (Sandbox Code Playgroud)

我需要让方法开始和结束迭代整个列表.

我尝试这种方式,但有了这个实现,我没有得到列表的最后一个节点.

iterator begin()
{
    return iterator(_head);
}

iterator end()
{
    return iterator(_last);
}
Run Code Online (Sandbox Code Playgroud)

谁能帮助我如何制作这两种方法?

PS对不起我的英文,我知道这不好.

感谢帮助

编辑:

我的Node类就是这个

class Node
{
public: 
    T _data;
    Node* _next;
};
Run Code Online (Sandbox Code Playgroud)

我用这个循环进行测试..

for (List<int>::iterator it = list->begin(); it != list->end(); it++)
{
    std::cout << *it << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

Sho*_*hoe 6

结束迭代器应该是"past-the-end"元素,而不是实际的最后一个元素.所以你应该真的:

iterator end()
{
    return iterator(nullptr);
}
Run Code Online (Sandbox Code Playgroud)

然后实现operator==为:

bool operator==(const iterator& other) { return _node == other._node; }
bool operator!=(const iterator& other) { !((*this) == other); }
Run Code Online (Sandbox Code Playgroud)

让它接受nullptr.