STL------List与迭代器的实现

更多C++知识点:C++目录索引


1. List

  List是STL库(标准模板库)中的一个容器。它的实质上就是一个双向带头循环链表,这样的设计有以下几个优点:
  

  1. 随机插入数据时,不用遍历链表,去找某一特定位置
  2. 尾插时,只需head->prev就可找到,提高了效率
  3. 在链表中删除数据时,也只需给出迭代器的位置即可,不需遍历链表找
    到其前一个,或者采用替换删除的方法删除节点

2. 迭代器

 迭代器是一种行为类似指针的对象,它提供类似指针的功能,对容器的内容进行走访,而指针的各种行为中最常见也最重要的便是“内容提领”和“成员访问”。因此,迭代器最重要的编程工作就是对operator*和operator->进行重载工作。

3. List的迭代器

  List 的迭代器中重载operator++ 和 operator– ,可以使迭代器向指针一样,按顺序访问链表的元素

4. 模拟实现

//链表的结点
template<class T>
struct ListNode  
{
    ListNode<T>* _next;
    ListNode<T>* _prev;
    T _data;

    ListNode(const T& x = T())
        :_data(x)
        , _next(nullptr)
        , _prev(nullptr)
    {}

};

//模板参数,定义为类型T,T的引用,T的指针
template<class T, class Ref, class Ptr>
struct MyIterator
{
    typedef ListNode<T> Node;//将node节点重名名为node
    typedef MyIterator<T, Ref, Ptr> Self;//将自己重命名为self
    Node* _node;

    MyIterator(Node* node)
        :_node(node)
    {}

    Ref operator*()//引用
    {
        return _node->_data;
    }

    Ptr operator->()//指针
    {
        return &(operator*());
    }

    Self& operator++()//前置++
    {
        _node = _node->_next;
        return *this;
    }

    Self operator++(int)//后置++
    {
        Self tmp(_node);
        _node = _node->_next;

        return tmp;
    }

    Self& operator--()//前置--
    {
        _node = _node->_prev;
        return *this;
    }

    Self operator--(int)//后置--
    {
        Self tmp(_node);
        _node = _node->_prev;
        return tmp;
    }
    bool operator==(const Self& s) const//const迭代器
    {
        return _node == s._node;
    }
    bool operator!=(const Self& s) const
    {
        return _node != s._node;
    }
};

template<class T>
class MyList
{
    typedef ListNode<T> Node;
public:
    typedef MyIterator<T, T&, T*> Iterator;//声明
    typedef MyIterator<T, const T&, const T*> ConstIterator;

    MyList()
    {
        _head = new Node;
        _head->_next = _head;
        _head->_prev = _head;
    }

    void Insert(Iterator pos, const T& x)
    {
        Node* cur = pos._node;
        Node* next = cur->_next;
        Node* newnode = new Node(x);

        //cur next newnode
        newnode->_next = next;
        next->_prev = newnode;

        newnode->_prev = cur;
        cur->_next = newnode;
    }

    void Erase(Iterator pos)
    {
        Node* cur = pos._node;
        Node* next = cur->_next;
        Node* prev = cur->_prev;

        //prev pos next
        prev->_next = next;
        next->_prev = prev;

        delete cur;
    }
    void PushBack(const T& x)
    {
        Node*  tail = _head->_prev;

        Node* newnode = new Node(x);

        newnode->_next = tail->_next;
        _head->_prev = newnode;

        tail->_next = newnode;
        newnode->_prev = tail;
    }


    Iterator Begin()//迭代器的开始
    {
        return _head->_next;
    }

    Iterator End()//迭代器的结束
    {
        return _head;
    }

    ConstIterator Begin() const
    {
        //return Iterator(_head->_next);
        return _head->_next;
    }

    ConstIterator End() const
    {
        //return Iterator(_head);
        return _head;
    }



protected:
    Node* _head;
};

测试代码:

void PrintList( MyList<int>& l)
{
    MyList<int>::Iterator it = l.Begin();
    while (it != l.End())
    {

        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

void Test()
{
    MyList<int> l;
    l.PushBack(1);
    l.PushBack(2);
    l.PushBack(3);
    l.PushBack(4);
    l.PushBack(5);

    PrintList(l);
    MyList<int>::Iterator it = l.Begin();
    while (it != l.End())
    {
        if ((*it._node)._data == 3)
        {
            l.Insert(it, 0);
            break;
        }
        ++it;

    }

    PrintList(l);

    while (it != l.End())
    {
        if ((*it._node)._data == 0)
        {
            l.Erase(it);
            break;
        }
        ++it;

    }

    PrintList(l);

}

猜你喜欢

转载自blog.csdn.net/zhangye3017/article/details/80739390