C++入门篇9---list

list是带头双向循环链表

一、list的相关接口及其功能

1. 构造函数

|-------------------------------------------------------|-------------------------------------|
| 函数声明 | 功能说明 |
| list(size_type n,const value_type& val=value_type()) | 构造的list中包含n个值为val的元素 |
| list() | 构造空的list |
| list(const list& x) | 拷贝构造 |
| list(InputIterator first, InputIterator last) | 用**[** fiirst,last**)**区间的元素构造list |

cpp 复制代码
void test1()
{
	list<int> v;
	list<int> v1(5,2);
	list<int> v2(v1);
	list<int> v3(v1.begin(),v1.end());
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	for (auto x : v1)
		cout << x << " ";
	cout << endl;

	for (auto x : v2)
		cout << x << " ";
	cout << endl;

	for (auto x : v3)
		cout << x << " ";
	cout << endl;

}

2.list的迭代器

|-----------------|---------------------------------------------------------------------------------------------------------|
| 函数名称 | 功能名称 |
| begin()+end() | 获取第一个数据位置的iterator/const_iterator,获取最后一个数据的下一个位置的iterator/const_iterator |
| rbegin()+rend() | 获取第一个数据位置的reverse_iterator/const_reverse_iterator,获取最后一个数据的下一位置的reverse_iterator/const_reverse_iterator |

cpp 复制代码
void test2()
{
	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();
	//注意如果写类型名,那么一定要写正确,如加不加reverse、const一定要写对
	//如果不想写这么长的类型,可以写auto自动类型推导
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	list<int>::reverse_iterator it1 = v.rbegin();
	while (it1 != v.rend())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
}

3.list的capacity

|---------|----------------|
| 函数声明 | 功能介绍 |
| empty() | 检测list是否为空 |
| size() | 返回list中有效结点的个数 |

4.获取首尾元素

|-------|--------------------|
| 函数声明 | 功能介绍 |
| front | 返回list的第一个节点中值的引用 |
| back | 返回list的最后一个结点中值的引用 |

5.list的修改

|------------|-----------------------|
| 函数名称 | 功能介绍 |
| push_front | 在list首元素前插入值为val的值 |
| pop_front | 删除list中第一个元素 |
| push_back | 在list尾部插入值为val的值 |
| pop_back | 删除list中的最后一个元素 |
| insert | 在list中pos位置插入值为val的元素 |
| erase | 删除list中pos位置的元素 |
| swap | 交换两个list中的元素 |
| clear | 清空list中的有效元素 |

cpp 复制代码
void test3()
{
	list<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_front(1);
	v.push_front(2);
	v.push_front(3);
	v.push_front(4);
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	v.pop_back();
	v.pop_front();
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	v.insert(v.begin(),10);
	for (auto x : v)
		cout << x << " ";
	cout << endl;

	v.erase(v.begin());
	for (auto x : v)
		cout << x << " ";
	cout << endl;
}

6.list迭代器失效问题(重点)

在讲vector的博客中,我也提到了迭代器失效问题,那么问个问题,list的迭代器失效和vector的迭代器失效一样吗?为什么?

这里先解释一下什么是迭代器,估计有很多人对这个名词还不是很了解,其实所谓的迭代器从作用上来说就是访问遍历容器的工具,它将所有容器的访问遍历方式进行了统一(vector,list,set等等容器的迭代器使用几乎一摸一样都是begin(),end(),++/--等操作),封闭了底层的细节,简化了我们对容器的使用,对于初学者来说,这玩意tm的太神了,但是如果我们了解它的底层实现,我们就会发现,迭代器不过是一层封装,底层还是数据结构那一套,如list链表,迭代器的++,本质还是指针的变化。

(容器的底层实现还是要了解一些,能够帮助我们更好的认识和使用容器,可以看看我写过的一些模拟实现,如果有需要注释或者详解,请在评论区留言,如果需求多,我会单独出一篇博客讲解一下里面的一些重点内容)

好,下面回归正题,如果你数据结构学的还不错并且知道vector的迭代器失效是扩容引起的,那么这个问题不难回答,因为链表的增查改不会影响一个结点的位置,除了删除操作,所以list的迭代器失效仅仅只有在删除list结点时才会出现,并且只有那个被删除结点的迭代器会失效,其他的不受影响

二、模拟实现list的基本功能

cpp 复制代码
namespace zjs
{
	template <class T>
	struct list_node {
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

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

    //重点
	template <class T, class Ref, class Ptr >
	struct __list_iterator {
		typedef list_node<T> Node;
		typedef __list_iterator self;
		Node* node;

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

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

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

		bool operator==(const self& It) const
		{
			return node == It.node;
		}

		bool operator!=(const self& It) const
		{
			return node != It.node;
		}

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

	/*template <class T>
	struct __list_const_iterator {
		typedef list_node<T> Node;
		typedef __list_const_iterator self;
		Node* node;

		__list_const_iterator(Node* x)
			:node(x)
		{}

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

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

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

		bool operator==(const self& It)
		{
			return node == It.node;
		}

		bool operator!=(const self& It)
		{
			return node != It.node;
		}
	};*/

	template <class T>
	class list
	{
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T,const T&,const T*> const_iterator;
		//typedef __list_const_iterator<T> const_iterator;
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			_size = 0;
			empty_init();
		}
		
		void clear()
		{
			iterator it = begin();
			while (it!=end())
			{
				it = erase(it);
			}
		}
		
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		list(const list<T>& tmp)
			:_head(nullptr)
			,_size(0)
		{
			empty_init();
			for (auto& x : tmp)
			{
				push_back(x);
			}
		}

		void swap(list& tmp)
		{
			std::swap(_head, tmp._head);
			std::swap(_size, tmp._size);
		}

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

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

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

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

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

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

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


		void pop_back()
		{
			erase(--end());
		}

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

		iterator erase(iterator pos)
		{
			Node* cur = pos.node;
			Node* pre = cur->_prev;
			Node* next = cur->_next;
			pre->_next = next;
			next->_prev = pre;
			delete cur;
			_size--;
			return next;
		}

		size_t size() const
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

    //模板的一些应用,typename的用法
    //这里只能用typedef,用来告诉编辑器const_iterator是一个类型名,而不是一个静态变量
    //因为编辑器在编译阶段要判断有没有语法错误,而list<T>没有实例化,就无法在里面
    //查找const_iterator,而如果它是静态变量很显然这是个语法错误,
    //所以这里要加上typename告诉编辑器这是个类型名,等到实例化之后再去里面找
	template<typename T>
	void print_list(const list<T>& s)
	{
		typename list<T>::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	template<typename container>
	void print_container(const container& s)
	{
		typename container::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

}
相关推荐
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py3 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy3 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ6 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6256 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林6 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
white__ice7 小时前
2024.9.19
c++