【C++】手撕 list类(包含迭代器)

目录


1,list的介绍及使用

1,list 是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2,list 的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。

3,list 与 forward_list 非常相似:最主要的不同在于 forward_list 是单链表,只能朝前迭代,已让其更简单高效。

4,与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。

5,与其他序列式容器相比,list 和 forward_list 最大的缺陷是不支持任意位置的随机访问,比如:要访问 list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list 还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

2,list_node

结点的结构体框架

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

因为是双向循环链表,需要一个上指针 _prev,下指针 _next,还有数据 data;

3,list_node()

对结点进行初始化

		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, data(x)
		{}

然后还要将其初始化,指针为空,数据为内置类型初始化的值;

3,list

链表结构框架

	template <class T>
	class list
	{
		typedef list_node<T> node;
	public:

	private:
		node* _head;
	};

链表是带头结点的,所以我们需要一个哨兵位头结点;

4,list()

对链表初始化

		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_init();
		}

因为我们是双向循环链表,所以我们的下一个结点和上一个结点都是指向自己的,形成一个环;

5,push_back(const T& x)

尾插

		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;
		}

我们先找到尾结点(tail),申请一个新结点,然后就插入其中;

6,print()

打印数据

		void print()
		{
			node* cur = _head->_next;
			while (cur != _head)
			{
				cout << cur->data << " ";
				cur = cur->_next;
			}
		}

哨兵位头结点本身是没有数据的,所以要从下一个结点开始

	void test1()
	{
		wxd::list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);

		lt1.print();
	}

也是没有任何问题的

7,_list_iterator

迭代器的框架和初始化


	template<class T,class ref,class ptr>
	struct _list_iterator
	{
		typedef list_node<T> node;
		typedef _list_iterator<T,ref,ptr> sefl;
		node* _node;

		_list_iterator(node* n)
			:_node(n)
		{}
    }

有人会好奇,为什么模板里面有三个参数,现在先不急下面会进行分晓的;

指向结点的迭代器嘛,底层类型就是指针;

初始化也是一样,传来什么就是什么;

8,operator*()

迭代器解引用取值

		ref operator*()
		{
			return _node->data;
		}

ref 其实就是 T&;

9,begin()

找头结点

		iterator begin()
		{
			return iterator(_head->_next);
		}

直接返回构造完后的结果;

10,end()

最后一个结点的下一个位置

		iterator end()
		{
			return iterator(_head);
		}

然后我们就可以试一下迭代器打印了;

	void print_list(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test1()
	{
		wxd::list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);

		print_list(lt1);
	}

11,operator->()

迭代器箭头指向取值

		ptr operator->()
		{
			return &_node->data;
		}

返回的是 data 的地址,ptr 是 T* ;

12,operator++()

迭代器前置++

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

sefl 是  _list_iterator<T,ref,ptr>;

13,operator++(int)

迭代器后置++

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

返回的是之前的值,但其实已经改变了;

14,operator–()

迭代器前置 – –

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

15,operator–(int)

迭代器后置 – –

		sefl operator--(int)
		{
			sefl tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

16,operator==(const sefl& s)

迭代器判断相等

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

判断迭代器是否相等比较 _node 就可以了;

17,operator!=(const sefl& s)

判断是否不相等

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

18,_list_const_iterator

然后这个是 const 迭代器版本的,这里我就不一个一个写了;

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

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

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

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

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

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

		sefl operator--(int)
		{
			sefl tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

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

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

	};

其实吧,_list_const_iterator 跟 _list_iterator 就是内部函数参数的返回值不同罢了,我们可以用模板参数来实例化,这样就不用写两个迭代器了;

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;

list 下面这样操作就可以了,普通迭代器模板一个版本,const 迭代器模板内的参数加上const就可以了,等调用的时候编译器会自动匹配的;

19,list(iterator first, iterator last)

迭代器区间构造

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

20,begin()const

const 版本取头结点

		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}

21,end()const

const 版本取尾结点的下一个位置

		const_iterator end()const
		{
			return const_iterator(_head);
		}

22,list(const list<T>& lt)

拷贝构造

		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}

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

先把要拷贝的区间信息构造另一个 list ,然后再与 this 指针的 _head哨兵位头结点进行交换即可;

23,operator=(list<T> lt)

赋值

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

24,insert(iterator pos, const T& x)

插入

		void insert(iterator pos, const T& 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;
		}

定义前一个结点,和本身的结点,然后再进行插入即可;

	void test3()
	{
		wxd::list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);

		auto pos = find(lt1.begin(), lt1.end(), 3);
		lt1.insert(pos, 9);
		print_list(lt1);
	}

25,erase(iterator pos)

擦除

		iterator erase(iterator pos)
		{
			assert(pos != end());

			node* next = pos._node->_next;
			node* tail = pos._node->_prev;

			tail->_next = next;
			next->_prev = tail;

			delete pos._node;
			return iterator(next);
		}

先断言一下,哨兵位结点是不能擦除的;

然后找到前一个结点,后一个结点,在进行互相绑定;

在释放要删除的空间;

26,clear()

清除

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

27,~list()

析构函数

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

先清空结点,然后再是否哨兵位头结点置空即可;

28,push_front(const T& x)

头插

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

直接用 insert 插入更加方便;

29,pop_front()

头删

		void pop_front()
		{
			erase(begin());
		}

30,pop_back()

尾删

		void pop_back()
		{
			erase(_head->_prev);
		}

31,源代码

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
	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> sefl;
		node* _node;

		_list_iterator(node* n)
			:_node(n)
		{}

		ref operator*()
		{
			return _node->data;
		}

		ptr operator->()
		{
			return &_node->data;
		}

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

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

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

		sefl operator--(int)
		{
			sefl tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

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

		bool operator!=(const sefl& 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;

		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;
			}
		}

		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;
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator end()const
		{
			return const_iterator(_head);
		}

		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}

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

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


		void insert(iterator pos, const T& 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)
		{
			assert(pos != end());

			node* next = pos._node->_next;
			node* tail = pos._node->_prev;

			tail->_next = next;
			next->_prev = tail;

			delete pos._node;
			return iterator(next);
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

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

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(_head->_prev);
		}

		void pop_front()
		{
			erase(begin());
		}

		void print()
		{
			node* cur = _head->_next;
			while (cur != _head)
			{
				cout << cur->data << " ";
				cur = cur->_next;
			}
		}


	private:
		node* _head;
	};

32,总结

我们就先搞一个大概的,其中还有很多分支,比如我们写的是擦除某个数据,其实也可以擦除某个范围,这些就靠大家去摸索,查阅文档了;

list 类的实现就到这里了;

加油!

版权声明:本文为博主作者:手法king原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/m0_71676870/article/details/135455272

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2024年1月16日
下一篇 2024年1月16日

相关推荐