当前位置: 首页 > news >正文

【手撕】list

系列文章目录


文章目录

  • 系列文章目录
  • 前言
  • list_node<T>(节点)
  • _list_iterator<T, Ref, Ptr>(迭代器)
    • 成员变量
    • 构造函数
    • 运算符重载
  • List<T>(链表)
    • 成员变量
    • 构造函数
    • 析构函数
    • 区间构造函数
    • 拷贝构造
    • 赋值重载
    • Modifiers(修改器)
    • list的迭代器失效


前言

模拟实现list类


STL3.0(SGI版本)

list_node(节点)

//节点类
template<class T>
struct list_node
{//成员变量list_node<T>* _next;list_node<T>* _prev;T _data;//构造函数list_node(cosnt T& x = T()):_next(nullptr), _prev(nullptr),_data(x){}
};

_list_iterator<T, Ref, Ptr>(迭代器)

成员变量

  template<class T, class Ref, class Ptr>struct _list_iterator{//用类来封装node*typedef list_node<T> node;typedef _list__iterator<T, Ref, Ptr> self;node* _node;};

构造函数

//构造函数
_list_iterator(node* n):_node(n)
{}

运算符重载

//Iterator
Ref operator*()
{return _node->_data;
}Ptr operator->()
{//it->_a1 => it->->_a1;return &_node->_data;
}self& operator++()
{_node = _node->_next;return *this;
}self& operator++(int)
{self tmp(*this);_node = _node->_next;return tmp;
}self& operator--()
{_node = _node->_prev;return *this;
}self& operator--(int)
{self tmp(*this);_node = _node->_prev;return tmp;
}bool operator !=(const self& s)
{return _node != s._node;
}bool operator ==(const self& s)
{return _node == s._node;
}

List(链表)

成员变量

template<class T>
class list
{typedef list_node<T> node;public:typedef _list_iterator<T, T&, T*> iterator;typedef _list_iterator<T, const T&, const T*>const_iterator;private:node* _head;//节点指针
};

构造函数

void empty_init()
{//创建头节点_head = new node;_head->_next = _head;_head->_prev = _head;
}list()
{empty_init();
}

析构函数

//析构函数
~list()
{	clear();//释放头节点delete _head;_head = nullptr;
}

区间构造函数

template <class Iterator>
list(Iterator first, Iterator last)
{empty_init();while (first != last){push_back(*first);++first;}
}

拷贝构造

void swap(list<T>& lt)
{std::swap(_head, lt._head);
}list(const list<T>& lt)
{empty_init();list<T> tmp(lt.begin(), lt.end());swap(tmp);
}

赋值重载

list<T>& operator=(list<T> tmp)
{swap(tmp);return *this;
}

Modifiers(修改器)

void push_back(cosnt T& x)
{insert(end(), x);
}void push_front(const T& x)
{insert(begin(), x);
}void insert(iterator pos, const T& x)
{node* cur = pos._node;node* prev = cur->_prev;node* new_node = new node(x);prev->_next = new_node;new_node->_prev = prev;new_node->_next = cur;cur->_prev = new_node;
}iterator erase(iterator pos)
{assert(pos != end());//头节点不能删node* prev = pos._node->_prev;node* next = pos._node->_next;prev->_next = next;next->_prev = prev;delete pos._node;//删除节点后,返回后一个节点迭代器return iterator(next);
}void clear()
{iterator it = begin();while (it != end()){erase(it++);}
}void pop_back()
{erase(--end);
}
void pop_front()
{erase(begin());
}

list的迭代器失效

void TestListIterator1()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array + sizeof(array) / sizeof(array[0]));auto it = l.begin();while (it != l.end()){// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值l.erase(it);++it;}
}
// 改正
void TestListIterator()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array + sizeof(array) / sizeof(array[0]));auto it = l.begin();while (it != l.end()){l.erase(it++); // it = l.erase(it);}
}
http://www.lryc.cn/news/98390.html

相关文章:

  • QMQTT快速入门
  • Dooring-Saas低代码技术详解
  • Linux chmod
  • java商城系统和php商城系统有什么差异?如何选择?
  • 【HTML】常用实体字符(如 nbsp; 空格)
  • 华为eNSP通过VMnet8虚拟网卡,NAT转换访问互联网
  • 手撕顺序表
  • Python实战项目——旅游数据分析(四)
  • 前端CryptoJS-AES加解密 对应php的AES-128-CBC加解密踩坑(java也相同加解密)
  • Python解码张三的法外狂徒之旅,揭秘视频背后的真相!【含jS逆向解密】
  • 【解析】对比学习和孪生网络的关系
  • Java版本企业工程项目管理系统平台源码(三控:进度组织、质量安全、预算资金成本、二平台:招采、设计管理)
  • 智能井盖:科技赋能城市脚下安全
  • wangeditor编辑器配置
  • Sqlite使用WAL模式指南
  • 一套高质量可靠的 React Hooks 库
  • 集合---list接口及实现类
  • JVM简述
  • 7.25训练总结
  • java注解@FeignClient修饰的类路径不在spring boot入口类所在的包下,有哪几种处理方式?
  • 神经网络随记-参数矩阵、剪枝、模型压缩、大小匹配、、
  • 4、Kubernetes 集群 YAML 文件详解
  • leetcode93. 复原 IP 地址(java)
  • 极光Java 版本服务器端实现别名消息推送
  • 【Lua学习笔记】Lua进阶——Table(4)继承,封装,多态
  • 安全性证明(四)Practical Identity-Based Encryption Without Random Oracles
  • Linux中常用的指令
  • 【java】【面对对象高级4】内部类、枚举、泛型
  • Python的用处到底是什么?(三)
  • 【Nodejs】Express基本使用