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

list的模拟实现

目录

1.结构

2.用3个类实现list

3.单个节点的定义

4.迭代器的定义

5.list类的实现


1.结构

list底层是一个带头双向循环链表

2.用3个类实现list

1.链表中的单个节点

2.迭代器

3.list

由于链表中的迭代器已经不是原生指针,所以将迭代器单独封装成一个类。

3.单个节点的定义

template<class T>
class listnode
{
public:
    T _data;
    listnode<T>* _prev;
    listnode<T>* _next;

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

};

4.迭代器的定义

//Ref是T的引用但不知道是不是const迭代器,所以写成模版的形式
//Ptr是T的指针但不知道是不是const迭代器,所以写成模版的形式
//这样就不用单独写一个const迭代器了
template<class T, class Ref, class Ptr>
class list_iterator
{
public:
    typedef listnode<T> Node;
	typedef list_iterator<T, Ref, Ptr> Self;

    Node* _node;


	//这里的构造函数是为了一会方便提供list类里的begin和end的接口
	//begin和end接口写在list类里是因为:在List类里才有真正的链表结构
	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--()
	{
		_node = _node->_prev;
		return *this;
	}

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

		return tmp;
	}

	Self& operator--(int)
	{
		Self tmp(*this);
		_node = _node->_prev;

		return tmp;
	}

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

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



};

5.list类的实现

template<class T>
class List
{
    typedef listnode<T> Node;
	
public:

	typedef list_iterator<T, T&, T*> iterator;
	typedef list_iterator<T, const T&, const T*> const_iterator;


	//构造哨兵位头结点
    List()
    {
        _head = new Node;
        _head->prev = _head;
        _head->next = _head;
        _size = 0;
    }

	iterator begin()
	{
		//隐式类型转换,利用迭代器类里的构造函数
		return _head->next;
	}

	iterator end()
	{
		//隐式类型转换
		return _head;
	}

	const_iterator begin() const
	{
		//隐式类型转换
		return _head->next;
	}

	const_iterator end() const
	{
		//隐式类型转换
		return _head;
	}

	void push_back(const T& x)
	{
		/*Node* newnode = new Node(x);
		Node* prev = _head->_prev;
		Node* cur = _head;
		newnode->_prev = prev;
		newnode->_next = cur;
		prev->_next = newnode;
		cur->_prev = newnode;*/

		insert(end(), x);
	}

	void insert(iterator pos, const T& x)
	{
		Node* prev = pos->_prev;
		Node* cur = pos;
		Node* newnode = new Node(x);
		newnode->_prev = prev;
		newnode->_next = cur;
		prev->_next = newnode;
		cur->_prev = newnode;
		_size++;
	}


	//返回被删除元素的下一个元素的迭代器,防止迭代器失效
	iterator erase(iterator pos)
	{
		Node* prev = pos->_prev;
		Node* next = pos->_next;
		prev->_next = next;
		next->_prev = prev;
		delete pos;
		_size--;
		return next;
	}

	//拷贝构造
	List(const List<T>& s1)
	{
		_head = new Node;
		_head->prev = _head;
		_head->next = _head;
		_size = 0;

		for (auto s : s1)
		{
			push_back(s);
		}

	}

	void swap(List<T> sl)
	{
		std::swap(_head, sl._head);
		std::swap(_size, sl._size);
	}



	List<T>& operator=(List<T> s1)
	{
		swap(s1);
		return *this;
	}

	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}

	~List()
	{
		clear();
		delete _head;
		_head = nullptr;

	}


	int size() const
	{
		return _size;
	}

	bool empty() const
	{
		return _size == 0;
	}


private:
    Node* _head;
    int _size;
};


http://www.kler.cn/news/328546.html

相关文章:

  • MySQL SQL基础常见面试题整理
  • PHP反序列化3(属性绕过)
  • 《PMI-PBA认证与商业分析实战精析》 第3章 需要评估
  • Typora使用与Markdown语法详细教程
  • 在Windows上安装Git
  • go+redis基于tcp实现聊天室
  • 8--苍穹外卖-SpringBoot项目中套餐管理 详解(二)
  • XR图和XS图中X-bar图的最大不同
  • uniapp 常用高度状态栏,导航栏,tab栏,底部安全高度
  • C++ 游戏开发
  • Docker 安装 Citus 单节点集群:全面指南与详细操作
  • Linux 文件目录结构(详细)
  • 【PostgreSQL】入门篇——如何创建、删除和管理数据库及其用户,包括权限设置和角色管理
  • OSPF路由计算
  • 滑动窗口->dd爱框框
  • Elasticsearch学习笔记(3)
  • Service Mesh
  • Java | Leetcode Java题解之第450题删除二叉搜索树中的节点
  • Arduino UNO R3自学笔记7 之 Arduino使用PWM电机调速
  • 服务器数据恢复—存储映射到服务器上的卷无法挂载的数据恢复案例
  • DC00025【含论文】基于协同过滤推荐算法springboot视频推荐管理系统
  • 使用Yasboot安装YashanDB的疑惑和建议
  • 进阶数据库系列(十三):PostgreSQL 分区分表
  • SolidWorks机器转ROS2 URDF
  • Linux下send函数和recv函数
  • AWS Redshift把老用户权限赋予新用户
  • 201 Created
  • 如何在Windows、Mac和Linux系统上安装和更新Stable Diffusion WebUI
  • Spark SQL分析层优化
  • 中国电信解锁万亿参数大模型:TeleAI的创新与突破