list类模拟实现

list类的模拟实现

目录

  1. 结点
  2. 框架
  3. 迭代器
    1. 初次尝试
    2. const迭代器

完整代码

1.结点

一个结点需要包含:储存的数据,上一个结点的地址,下一个结点的地址。

c++ 复制代码
template<class T>
struct __list_node
{
    __list_node<T>* _prev;
    __list_node<T>* _next;
    T _data;

    __list_node(const T& x = T())	//T()是匿名类型,如果没有传参,编译器会调用相应类型的构造函数
        :_data(x)
        ,_prev(nullptr)
        ,_next(nullptr)
    {}
};
  • 由于我们后面会在类外访问,所以为了避免麻烦就写成struct,这样成员属性都是public的。
  • 使用模板,方便适配各种数据类型

2.框架

list类包含哨兵结点的指针

c++ 复制代码
template<class T>
class list
{
public:
    typedef __list_node<T> Node;
    
    list()
    {
        _head = new Node;
        _head->_next = _head;
        _head->_prev = _head;
    }
    
private:
    Node* _head;
};    
  • 因为是双向循环链,所以哨兵结点的指针都指向自己

3.迭代器

list的迭代器的实现,是模拟list实现的重中之重

3.1初次尝试

  • list迭代器的本质还是指针
  • 我们将指针的封装,内部进行运算符重载
c++ 复制代码
template<class T>
struct __list_iterator
{
    typedef __list_node<T> Node;
    typedef __list_iterator<T> Self;
    Node* _node;

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

    T& operator*()
    {
        return _node->_data;
    }

    T* operator->()
    {
        return &_node->_data;
    }

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

    Self operator++(int)
    {
        Self tmp(*this);
        //_node = _node->_next;
        ++(*this);
        return tmp;
    }

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

    Self operator--(int)
    {
        Self tmp(*this);
        //_node = _node->_next;
        --(*this);
        return tmp;
    }

    bool operator!=(const Self& it)const
    {
        return _node != it._node;
    }

    bool operator==(const Self& it)const
    {
        return _node == it._node;
    }
};
  • 就是将++/--的操作通过运算符重载,变成在结点间移动的操作。

3.2const迭代器

  • list的迭代器不像vector/string类的迭代器,它不可以直接在迭代器的前面加const修饰
  • 指针被const修饰后,不能通过指针来修改数据,意味着operator*operator->的作用失效,但其他函数还能继续使用
  • 如果再专门写一份const的迭代器,那么代码就太过冗余了
c++ 复制代码
template<class T, class Ref, class Ptr>
struct __list_iterator
{
    typedef __list_node<T> Node;
    typedef __list_iterator<T, Ref, Ptr> Self;
    Node* _node;

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

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

    Ptr operator->()
    {
        return &_node->_data;
    }

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

    Self operator++(int)
    {
        Self tmp(*this);
        //_node = _node->_next;
        ++(*this);
        return tmp;
    }

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

    Self operator--(int)
    {
        Self tmp(*this);
        //_node = _node->_next;
        --(*this);
        return tmp;
    }

    bool operator!=(const Self& it)const
    {
        return _node != it._node;
    }

    bool operator==(const Self& it)const
    {
        return _node == it._node;
    }
};
  • 这样,如果是const迭代器,就传const修饰的指针和引用。
相关推荐
Nobkins3 分钟前
2023CCPC河南省赛暨河南邀请赛个人补题ABEFGHK
开发语言·数据结构·c++·算法·图论
王RuaRua8 分钟前
[数据结构]7. 堆-Heap
c语言·数据结构·算法·链表
珊瑚里的鱼40 分钟前
第九讲 | 模板进阶
开发语言·c++·笔记·visualstudio·学习方法·visual studio
riri19191 小时前
算法分析:蛮力法
数据结构·算法
摄殓永恒1 小时前
猫咪几岁
数据结构·c++·算法
.小墨迹2 小时前
Apollo学习——键盘控制速度
linux·开发语言·c++·python·学习·计算机外设
似水এ᭄往昔2 小时前
【数据结构】——队列
c语言·数据结构·c++·链表
烛九_阴2 小时前
【C++】解析C++面向对象三要素:封装、继承与多态实现机制
c++
水水沝淼㵘2 小时前
嵌入式开发学习日志(数据结构--双链表)Day21
c语言·数据结构·学习·算法·排序算法
ai.Neo3 小时前
牛客网NC22012:判断闰年问题详解
开发语言·c++·算法