【C++】list的模拟实现

这篇具有很好参考价值的文章主要介绍了【C++】list的模拟实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.list 底层

list为任意位置插入删除的容器,底层为带头双向循环链表

c++ listnode赋值,C++,c++,list,链表

begin() 代表第一个结点,end()代表最后一个结点的下一个

2. list的模拟实现

1. list_node 类设计

template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

	};
c++ listnode赋值,C++,c++,list,链表

C++中,Listnode作为类名,而next和prev都是类指针,指针引用成员时使用->,而对象引用成员时使用 .
通过显示实例化,将两个类指针指定类型为T


2. list类如何调用类型

c++ listnode赋值,C++,c++,list,链表

Listnode 代表类型
Listnode 代表类名 + 模板参数 才是类型
而_head 以及创建新节点前都需加上类型

3 .push_back(正常实现)

void push_back(const T&x)//尾插
        {
            node* newnode = new node(x);
            node* tail = _head->_prev;
            tail->_next = newnode;
            newnode->_prev = tail;
            newnode->_next = _head;
            _head->_prev = newnode;
        }
c++ listnode赋值,C++,c++,list,链表

当我们想要创建一个节点时,需要调用node的构造函数
typedef list_node node ,node是由 list_node 类提供的


list_node(const T& x=T())//list类的构造函数
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{
		}

最好在构造函数处提供全缺省,对于内置类型int可以使用0,但对于自定义类型来说就不可以,所以为了满足泛型的要求,使用匿名对象调用默认构造函数

4. 迭代器的实现

c++ listnode赋值,C++,c++,list,链表

若在数组上有一个int类型的指针,解引用是int类型的数据,再++可以加载下一个位置,因为物理空间是连续的


c++ listnode赋值,C++,c++,list,链表

同样若在链表上,解引用类型为 node,再++不能到下一个节点,因为物理空间不连续
所以构造迭代器通过封装节点的指针来进行构造 (后面会讲)

第一个模板参数T

创建一个_list_iterator的类,来实现迭代器功能


template<class T>
    struct _list_iterator
    {
        typedef list_node<T> node;
        typedef _list_iterator<T> self;
        node* _node;
        
        _list_iterator(node* n)
            :_node(n)
        {

        }
        T& operator*()//解引用 
        {
            return _node->_data;
        }
        _list_iterator<T>& operator++()//返回迭代器
        {
            _node = _node->_next;//指向下一个节点
            return *this;
        }
        bool operator!=(const self&s)
        {
            return _node != s._node;
        }
    };

在list类中,调用迭代器实现begin()和end()功能
typedef _list_iterator<T,T&,T*> iterator,
通过typedef 将_list_node类模板定义为iterator


iterator begin()
		{
			return iterator(_head->_next);//通过匿名对象访问第一个数据
		}
		iterator end()
		{
			return iterator(_head);//通过匿名对象访问最后一个数据的下一个
		}

在list类中实现begin()和end(),内部调用_list_node类的构造函数


c++ listnode赋值,C++,c++,list,链表

const迭代器

c++ listnode赋值,C++,c++,list,链表

假设第一个代表的是T * ,而第二个代表的 T * const,保护迭代器本身不可以被修改,而我们想要保护迭代器指向的内容不可被修改即 const T*


复制_list_iterator类中的内容,并将名字修改为const_list_iterator
只需修改*operator类型为cosnt T& ,说明解引用后的数据返回不能被修改


template<class T>
    struct _list_const_iterator
    {
        typedef list_node<T> node;
        typedef _list_const_iterator<T> self;
        node* _node;

        _list_const_iterator(node* n)
            :_node(n)
        {

        }
        const T& operator*()//解引用 
        {
            return _node->_data;
        }
        self& operator++()//前置++
        {
            _node = _node->_next;//指向下一个节点
            return *this;
        }
        self& operator++(int)//后置++
        {
            self ret = *this;
            _node = _node->_next;
            return ret;
        }
        self& operator--()//前置--
        {
            _node = _node->_prev;
            return *this;
        }
        self& operator--(int)//后置--
        {
            self ret = *this;
            _node = _node->_prev;
            return ret;
        }
        bool operator!=(const self& s)//!=
        {
            return _node != s._node;
        }
        bool operator==(const self& s)//==
        {
            return _node == s._node;
        }
    };

c++ listnode赋值,C++,c++,list,链表

第二个模板参数Ref

迭代器和const迭代器只有 *operator 的返回值不同,
只需在模板中添加一个参数即可使用一个迭代器实现迭代器和const 迭代器的功能

c++ listnode赋值,C++,c++,list,链表


c++ listnode赋值,C++,c++,list,链表

第三个模板参数Ptr

对于内置类型int使用解引用找到对应数据,而自定义类型需使用->寻找下一个节点


c++ listnode赋值,C++,c++,list,链表

AA作为自定义类型,想要找到下一个节点需要使用->,在迭代器中 重载 - >

it->_a1,实际上是 it->->_a1,
it->返回值为AA* ,再通过这个指针使用->指向_a1
但是为了增强可读性,省略了一个->
it->_a1 实际上为 it.operator->()->._a1


c++ listnode赋值,C++,c++,list,链表


c++ listnode赋值,C++,c++,list,链表

对list封装的理解
c++ listnode赋值,C++,c++,list,链表

在不考虑封装的情况下,两者等价


c++ listnode赋值,C++,c++,list,链表

从物理空间上来看,it与pnode都是指向1的地址


c++ listnode赋值,C++,c++,list,链表

pnode作为一个原生指针,解引用指针就会拿到这个地址,找到这个地址指向空间的内容
++pnode,找到下一个节点的地址,但是下一个节点不一定是要的节点
*it 识别成为自定义类型就会调用函数

5. insert

void insert(iterator pos,const T&x)//在pos位置前插入x
        {
            node* cur = pos._node;
            node* prev = cur->_prev;
            node* newnode = new node(x);
            prev->_next = newnode;
            newnode->_prev = prev;
            newnode->_next = cur;
            cur->_prev = newnode;
        }
c++ listnode赋值,C++,c++,list,链表c++ listnode赋值,C++,c++,list,链表

6.push_back与 push_front(复用)

两者都可以通过复用 insert的方式实现

void push_back(const T&x)//尾插
        {
            /*node* tail = _head->_prev;
            node* newnode = new node(x);
            tail->_next = newnode;
            newnode->_prev = tail;
            newnode->_next = _head;
            _head->_prev = newnode;*/
            insert(end(), x);
        }
        void push_front(const T&x)//头插
        {
            insert(begin(), x);
        }
        

7. erase

void erase(iterator pos)//删除pos位置
        {
            //头节点不可以删除
            assert(pos != end());
            node* cur = pos._node;
            node* prev = cur->_prev;
            node* next = cur->_next;
            prev->_next = next;
            next->_prev = prev;
            delete cur;
        }
c++ listnode赋值,C++,c++,list,链表

由于头节点不可以删除,所以使用assert断言的方式

8. pop_back与pop_front (复用)

                void pop_back()//尾删
        {
            erase(--end());
        }
        void pop_front()//头删
        {
            erase(begin());
        }

9. clear 清空数据

void clear()//清空数据
            //但是要注意不要把head清掉
        {

            iterator it = begin();
            while (it != end())
            {
                it=erase(it);//为了防止迭代器失效设置返回值
                 //返回值为当前节点的下一个
            }
        }

迭代器失效是指迭代器所指向的节点失效 即节点被删除了
erase函数执行后,it所指向的节点被删除,因此it无效,在下一次使用it时,必须先给it赋值


c++ listnode赋值,C++,c++,list,链表

为了防止迭代器失效所以使erase设置返回值,返回值为当前节点的下一个

10. 迭代器区间构造

void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
template <class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

想要尾插的前提时,需要有头节点的存在,所以设置一个函数对头节点初始化


12. 拷贝构造

传统写法
void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
list(const list<T>& It)//拷贝构造  传统写法
        {
            empty_init();//初始化头节点
            for (auto e : It)
            {
                push_back(e);
            }
        }
c++ listnode赋值,C++,c++,list,链表
现代写法
void swap(list<T>& tmp)
        {
            std::swap(_head, tmp._head);
        }
        list(const list<T>& It)//拷贝构造现代写法
        {
            empty_init();//将头节点初始化
            list<T> tmp(It.begin(), It.end());
            swap(tmp);
        }

通过调用std中的swap来达到交换的目的

13. 赋值

void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}
list<T>& operator=(list<T> It)
        {
            swap(It);
            return *this;
        }

参数不可以使用 list &,虽然可以达到赋值的目的,但是It的值也会发生改变


c++ listnode赋值,C++,c++,list,链表


c++ listnode赋值,C++,c++,list,链表文章来源地址https://www.toymoban.com/news/detail-782951.html

3.完整代码

#include<iostream>
#include<list>
#include<assert.h>
using namespace std;
namespace yzq
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x=T())//构造函数
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{
		}
	};
	//迭代器
	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* n)
			:_node(n)
		{

		}
		Ref operator*()//解引用 
		{
			return _node->_data;
		}
		Ptr operator->()//->
		{
			return &_node->_data;
		}
		self& operator++()//前置++
		{
			_node = _node->_next;//指向下一个节点
			return *this;
		}
		self& operator++(int)//后置++
		{
			self ret = *this;
			_node = _node->_next;
			return ret;
		}
		self& operator--()//前置--
		{
			_node = _node->_prev;
			return *this;
		}
		self& operator--(int)//后置--
		{
			self ret = *this;
			_node = _node->_prev;
			return ret;
		}
		bool operator!=(const self&s)//!=
		{
			return _node != s._node;
		}
		bool operator==(const self& s)//==
		{
			return _node == s._node;
		}
	};
	//const迭代器
	//template<class T>
	//struct _list_const_iterator
	//{
	//	typedef list_node<T> node;
	//	typedef _list_const_iterator<T> self;
	//	node* _node;

	//	_list_const_iterator(node* n)
	//		:_node(n)
	//	{

	//	}
	//	const T& operator*()//解引用 
	//	{
	//		return _node->_data;
	//	}
	//	self& operator++()//前置++
	//	{
	//		_node = _node->_next;//指向下一个节点
	//		return *this;
	//	}
	//	self& operator++(int)//后置++
	//	{
	//		self ret = *this;
	//		_node = _node->_next;
	//		return ret;
	//	}
	//	self& operator--()//前置--
	//	{
	//		_node = _node->_prev;
	//		return *this;
	//	}
	//	self& operator--(int)//后置--
	//	{
	//		self ret = *this;
	//		_node = _node->_prev;
	//		return ret;
	//	}
	//	bool operator!=(const self& s)//!=
	//	{
	//		return _node != s._node;
	//	}
	//	bool operator==(const self& s)//==
	//	{
	//		return _node == s._node;
	//	}
	//};

	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;//const迭代器
		//typedef _list_const_iterator<T> const_iterator;
		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()//构造函数
		{
			empty_init();
		}
		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		//list(const list<T>& It)//拷贝构造
		//{
		//	empty_init();//初始化头节点
		//	for (auto e : It)
		//	{
		//		push_back(e);
		//	}
		//}
		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}
		list(const list<T>& It)//拷贝构造现代写法
		{
			empty_init();//将头节点初始化
			list<T> tmp(It.begin(), It.end());
			swap(tmp);
		}
		list<T>& operator=(list<T> It)//赋值
		{
			swap(It);
			return *this;
		}
		~list()//析构函数
		{
			//将头节点也要释放掉
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()//清空数据
			//但是要注意不要把head清掉
		{

			iterator it = begin();
			while (it != end())
			{
				it=erase(it);//为了防止迭代器失效设置返回值
				 //返回值为当前节点的下一个
			}
		}

		
		void push_back(const T&x)//尾插
		{
			/*node* tail = _head->_prev;
			node* newnode = new node(x);
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), x);
		}
		void push_front(const T&x)//头插
		{
			insert(begin(), x);
		}
		void insert(iterator pos,const T&x)//在pos位置前插入x
		{
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* newnode = new node(x);
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
		}
		iterator erase(iterator pos)//删除pos位置
		{
			//头节点不可以删除
			assert(pos != end());
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);
		}
		void pop_back()//尾删
		{
			erase(--end());
		}
		void pop_front()//头删
		{
			erase(begin());
		}
		iterator begin()
		{
			return iterator(_head->_next);//通过匿名对象访问第一个数据
		}
		iterator end()
		{
			return iterator(_head);//通过匿名对象访问最后一个数据的下一个
		}
		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end()const
		{
			return const_iterator(_head);
		}
	private:
		node* _head;
	};
	
	/*void test(const list<int>&e)
	{
		list<int>::const_iterator it = e.begin();
			while (it != e.end())
			{
					cout << *it << " ";
				++it;
			}
			cout << endl;
	}
	void test2()
	{
		const list<int> v;
		test(v);

	}*/
	//void test1()
	//{
	//	list<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);
	//	list<int>::iterator it= v.begin();
	//	while (it != v.end())
	//	{
	//		cout << *it << " ";
	//		++it;
	//	}
	//	cout << endl;
	//}
	/*struct AA
	{
		int	_a1;
		int _a2;
		AA(int a1=0,int a2=0)
			:_a1(a1)
			,_a2(a2)
		{
		}
	};*/
	/*void test3()
	{
		list<AA>v;
		v.push_back(AA(1, 1));
		v.push_back(AA(2, 2));
		v.push_back(AA(3, 3));
		list<AA>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << it->_a1 << " :"<<it->_a2<<" ";
			cout << endl;
			it++;
		}
	}*/
	//void test4()
	//{
	//	list<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);// 1 2 3 4
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//	v.clear();
	//	v.push_back(4);//  4
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//}
	//void test4()
	//{
	//	list<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);// 1 2 3 4
	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//	list<int> v2(v);
	//	for (auto e : v2)// 1 2 3 4
	//	{
	//		cout << e << " ";
	//	}
	//}
	void test4()
	{
		list<int> v;
		v.push_back(1);
		v.push_back(2);//v1 = 1 2
		list<int> v2;
		v2.push_back(5);
		v2.push_back(6);//v2=5 6
			v2 = v;
		for (auto e : v2)// v2=1 2 
		{
			cout << e << " ";
		}
		cout << endl;
		for (auto e : v)// v1=1 2
		{
			cout << e << " ";
		}
	}
}	

int main()
{
	yzq::test4();
	return 0;
}

到了这里,关于【C++】list的模拟实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++】list模拟实现

    个人主页 : zxctscl 如有转载请先通知 在前面一篇博客中分享了list的相关介绍 【C++】list介绍,这次来模拟实现一下list。 成员变量: 无参构造: 插入: 在库里面定义节点需要全部公有时用到的就是struct: 这里我们也用相同方法自己定义出一个节点: 然后在写list类时候就要

    2024年04月11日
    浏览(24)
  • C++ list 模拟实现

      目录 1. 基本结构的实现 2.  list() 3. void push_back(const T val) 4. 非 const 迭代器 4.1 基本结构  4.2 构造函数  4.3 T operator*() 4.4  __list_iterator operator++() 4.5 bool operator!=(const __list_iterator it) 4.6 T* operator-() 5. const 迭代器  6. begin()  end() ​编辑 7. iterator insert(iterator pos, const T v

    2024年02月08日
    浏览(34)
  • (C++) list底层模拟实现

     个人主页:Lei宝啊  愿所有美好如期而遇 首先,list底层是一个带头双向循环链表,再一个,我们还要解决一个问题,list的迭代器,vector和string的迭代器可以直接++,是因为他们的地址空间是连续的,而链表不是,所以链表的迭代器封装的不是原生指针,我们需要想办法解决

    2024年01月21日
    浏览(33)
  • [C++]:12:模拟实现list

    1.节点结构: 1.SGI下的节点通过两个结构体实现。 2.基础的链表节点只包括前驱指针和后继指针。 3.链表节点去继承基础链表节点,新增节点数据。 4.优化掉指针类型带模板参数。 2.节点构造函数: 1.节点本身在这个地方是没有构造函数的。 2.观察下面的链表的结构和链表的

    2024年01月22日
    浏览(31)
  • 《C++ list的模拟实现》

    本文主要介绍list容器的模拟实现 list示意图: 首先需要定义一个节点 的结构体 我们之前所理解的是:迭代器理解为像指针一样的东西,但是在list中有些不同 我们可以来观察一下STL源码中大佬是怎么封装的: 我们可以看到,只有一个成员,那就是一个结点的指针node,link_

    2024年02月07日
    浏览(29)
  • 【C++】list的模拟实现

    list为任意位置插入删除的容器,底层为带头双向循环链表 begin() 代表第一个结点,end()代表最后一个结点的下一个 1. list_node 类设计 C++中,Listnode作为类名,而next和prev都是类指针,指针引用成员时使用-,而对象引用成员时使用 . 通过显示实例化,将两个类指针指定类型为T

    2024年02月02日
    浏览(33)
  • 【STL源码分析】c++,List双向链表源码分析。自己实现list双向链表。

    参考链接:https://blog.csdn.net/man_sion/article/details/71003095? 先抽取要实现的功能,由于迭代器有些麻烦,就不使用了。要实现的功能有,push_back,pop_back,insert(指定位置,指定值),insert(指定位置,list,区间值),reverse,clear,getsize,begin,end,构造和析构函数,empty。 相关力扣题目:设计

    2024年02月03日
    浏览(32)
  • 【C++学习手札】模拟实现list

    ​                                                        🎬 慕斯主页 : 修仙—别有洞天                                                        ♈️ 今日夜电波 : リナリア—まるりとりゅうが                                            

    2024年02月05日
    浏览(33)
  • C++——list类及其模拟实现

    前言:这篇文章我们继续进行C++容器类的分享—— list , 也就是数据结构中的链表 ,而且是 带头双向循环链表 。 由于要满足存储任意类型的数据,所以我们必须要使用模版来进行定义 。  关于list类中的最难之处,就是 迭代器 了。 因为 迭代器的原理即为指针 ,对于 st

    2024年04月10日
    浏览(31)
  • 【C++修炼之路】list 模拟实现

    👑作者主页:@安 度 因 🏠学习社区:StackFrame 📖专栏链接:C++修炼之路

    2024年02月16日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包