【C++】五 、 list

目录

一、list的介绍及使用

二、list常用的接口说明

1.list常见的构造

2.list的访问及遍历操作

3.list的容量操作

4.list的增删查改

5.迭代器失效问题

三、 list的模拟实现

四、 list与vector的对比


一、list的介绍及使用

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

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

其前一个元素和后一个元素。

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

效。

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

更好。

5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list

的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间

开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这

可能是一个重要的因素)

二、list常用的接口说明

1.list常见的构造

#include <iostream>
#include <list>
int main ()
{
 std::list<int> l1; // 构造空的l1
 std::list<int> l2 (4,100); // l2中放4个值为100的元素
 std::list<int> l3 (l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
 std::list<int> l4 (l3); // 用l3拷贝构造l4
 // 以数组为迭代器区间构造l5
 int array[] = {16,2,77,29};
 std::list<int> l5 (array, array + sizeof(array) / sizeof(int) );
 // 用迭代器方式打印l5中的元素
 for(std::list<int>::iterator it = l5.begin(); it != l5.end(); it++)
 std::cout << *it << " ";
 std::cout<<endl;
 
 // C++11范围for的方式遍历
 for(auto& e : l5)
 std::cout<< e << " ";
 
 std::cout<<endl;
 return 0; 
}

 

2.list的访问及遍历操作

#include <iostream>
using namespace std;
#include <list>
void print_list(const list<int>& l) {
 // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
 for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
 {
 cout << *it << " ";
 // *it = 10; 编译不通过
 }
 
 cout << endl; }
int main()
{
 int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 list<int> l(array, array + sizeof(array) / sizeof(array[0]));
 // 使用正向迭代器正向list中的元素
 for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
 cout << *it << " ";
cout << endl;
 // 使用反向迭代器逆向打印list中的元素
 for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)
 cout << *it << " ";
 cout << endl;
 return 0; 
}

 

3.list的容量操作

 

4.list的增删查改

#include <list>
void PrintList(list<int>& l) 
{
 for (auto& e : l)
 cout << e << " ";
 cout << endl; }
//=====================================================================================

// push_back/pop_back/push_front/pop_front
void TestList1()
{
 int array[] = { 1, 2, 3 };
 list<int> L(array, array+sizeof(array)/sizeof(array[0]));
 // 在list的尾部插入4,头部插入0
 L.push_back(4);
 L.push_front(0);
 PrintList(L);
 // 删除list尾部节点和头部节点
 L.pop_back();
 L.pop_front();
 PrintList(L);
}
//=====================================================================================

// insert /erase 
void TestList3()
{
 int array1[] = { 1, 2, 3 };
 list<int> L(array1, array1+sizeof(array1)/sizeof(array1[0]));
 // 获取链表中第二个节点
 auto pos = ++L.begin();
 cout << *pos << endl;
 // 在pos前插入值为4的元素
 L.insert(pos, 4);
 PrintList(L);
 // 在pos前插入5个值为5的元素
 L.insert(pos, 5, 5);
 PrintList(L);
 // 在pos前插入[v.begin(), v.end)区间中的元素
 vector<int> v{ 7, 8, 9 };
 L.insert(pos, v.begin(), v.end());
 PrintList(L);
// 删除pos位置上的元素
 L.erase(pos);
 PrintList(L);
 // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
 L.erase(L.begin(), L.end());
 PrintList(L);
}
// resize/swap/clear
void TestList4()
{
 // 用数组来构造list
 int array1[] = { 1, 2, 3 };
 list<int> l1(array1, array1+sizeof(array1)/sizeof(array1[0]));
 PrintList(l1);
 // 交换l1和l2中的元素
 l1.swap(l2);
 PrintList(l1);
 PrintList(l2);
 // 将l2中的元素清空
 l2.clear();
 cout<<l2.size()<<endl; }

5.迭代器失效问题

迭代器失效即迭代器所指向的节点的无效,即该节 点被删除了因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响

 

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())
 {
 // 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())
 {
  it = l.erase(it);
  it++;
 }
}

 

三、 list的模拟实现

 

namespace hdzc
{
	template<class T>
	struct __list_node
	{
		__list_node<T>* _prev;
		__list_node<T>* _next;
					  T _data;

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

	template<class T, class Ref, class Ptr>//3个模板参数 引用 指针
	struct __list_iterator
	{
		typedef __list_iterator<T, Ref, Ptr> Self;//Self是自己定义的名称
		typedef __list_node<T> node;
		node* _node;

		__list_iterator(node* node)//结点的指针
			:_node(node)
		{}

		// 链表的迭代的核心还是节点的指针
		// 但是封装在类,可以通过运算符去控制++ * 等行为
		//T& operator*() 返回的是对象
		Ref operator*()
		{
			return _node->_data;
		}

		// T* operator->()返回的是指针
		Ptr operator->()
		{
			return &_node->_data;
		}

		Self& operator++()//后置++
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)//前置++
		{
			__list_iterator<T> 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& it)
		{
			return _node != it._node;
		}

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

	template<class T>
	class list
	{
		typedef __list_node<T> node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;//3个模板参数
		typedef __list_iterator<T, const T&, const T*>  const_iterator; // const迭代器
		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);
		}

		list()//构造函数
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		// list<int> lt2(lt1)
		list(const list<int>& lt)//拷贝构造
		{
			_head = new node;//先找出头结点 并让它自己指向自己 此时没有数据
			_head->_next = _head;
			_head->_prev = _head;

			for (auto e : lt)//往lt2里面遍历lt1
				push_back(e);
		}

		// lt1 = lt3; operator=
		/*	list<T>& operator=(const list<int>& lt) //方法一
		{
		if (this != &lt)//lt1去调clear  lt相当于lt3
		{
		clear();
		for (auto e : lt)
		{
		push_back(e);
		}
		}

		return *this;
		}
		*/
		// lt1 = lt3
		list<T>& operator=(list<int> lt)//方法二  现代写法
		{
			swap(_head, lt._head);//深拷贝 交换 lt1的head和lt3的交换 出作用域后lt3进行析构

			return *this;
		}


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

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				iterator next = it;
				++next;

				delete it._node;

				it = next;
			}

			_head->_next = _head;//恢复成最开始的状态
			_head->_prev = _head;
		}

		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;*/

			insert(end(), x);//因为在双向循环链表end的位置在head 所以在end前面插入即为尾插
		}

		void pop_back()//尾删 先--找到最后一个位置 再erase
		{
			erase(--end());
		}

		void push_front(const T& x)//在begin前面插入
		{
			insert(begin(), x);
		}

		void pop_front()//删除begin位置上的数
		{
			erase(begin());
		}

		void insert(iterator pos, const T& x)//在pos前的位置插入一个值 定义为newnode
		{
			node* cur = pos._node;//直接可以访问当前的node
			node* prev = cur->_prev;
			node* newnode = new node(x);

			// prev newnode cur 把3个结点连接起来
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());//不能删除头结点

			node* del = pos._node;//要删除的结点
			node* prev = del->_prev;
			node* next = del->_next;

			prev->_next = next;
			next->_prev = prev;
			delete del;

			return iterator(next);//返回删除这个地方下一个位置的迭代器 否则迭代器会失效
		}
	private:
		node* _head;
	};

 

四、 list与vector的对比

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值