list详解

介绍:

list是带头双向循环链表,在链表的任意位置删除插入效率高,但不能像vector一样可以通过下标随机访问每个位置的元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,

list的使用(接口)

1、构造函数

list的打印:迭代器打印。范围auto打印

2、list 的iterator(迭代器)

begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动

rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

其他一些接口:


迭代器失效:list在insert时迭代器不会失效,但在erase时迭代器会失效

list的模拟实现:

cpp 复制代码
#pragma once

#include<assert.h>
namespace bit
{
	template<class T>
		struct ListNode //构建每个链表节点
		{
			ListNode<T>* _next;
			ListNode<T>* _prev;
			T _date;

			ListNode(const T& x=T())//初始化节点
				:_next(nullptr)
				, _prev(nullptr)
				,_date(x)
			{
			}
		};
		
		template<class T>
		struct L //封装节点指针作为迭代器指针
		{
			typedef ListNode<T> Node;
			Node* _node;
			L(Node* node)
				:_node(node)
			{
				
			}

			//++it
			L& operator++()
			{
				_node = _node->_next;
				return *this;
			}
			//it++
			L& operator++(int)
			{
				L tmp = *this;
				_node = _node->_next;
				return tmp;
			}
			L& operator--()
			{
				_node = _node->_prev;
				return *this;
			}
			L& operator--(int)
			{
				L tmp = *this;
				_node = _node->_prev;
				return tmp;
			}

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

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

				
		};


		
		
		template<class T>
		class List
		{
			typedef ListNode<T> Node;
		public:
			typedef L<T> iterator;
			iterator begin()
			{
				return iterator(_head->_next);
			}
			iterator end()
			{
				return iterator(_head);
			}
			//默认构造函数
			List()
			{
				_head = new Node;
				_head->_next = _head;
				_head->_prev = _head;
			}
			//拷贝构造
			//ist(const List<T>& lt)
				List( List<T>& lt)
			{
				_head = new Node;
				_head->_next = _head;
				_head->_prev = _head;
				for (const auto& e : lt)
				{
					push_back(e);
				}
				
			}
			//尾插
			void push_back(const T& x=T())
			{
				/*Node* newnode = new Node(x);

				Node* tail = _head->_prev;
				tail->_next = newnode;
				newnode->_prev = tail;
				newnode->_next = _head;
				_head->_prev = newnode;*/
				insert(end(), x);


			}
			//头插
			void push_front(const T& x = T())
			{
				insert(begin(), x);
				
			}

			//插入
			iterator 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;
				return iterator(newnode);

			}
			//删除pos位置的节点
			iterator erase(iterator 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 next;
			}
			//尾删
			void  pop_back()
			{
				erase(--end());
			}

			//头删
			void pop_front()
			{
				erase(begin());
			}
			void clear()
			{
				iterator it = begin();
				while (it != end())
				{
					it = erase(it);
				}
			}
			~List()
			{
				clear();
				delete _head;
				_head = nullptr;

			}
			void swap(List<T>& tmp)
			{
				std::swap(_head, tmp._head);
			}
			List<T>& operator=(List<T> lt)
			{
				swap(lt);
				/*if (this != &lt)
				{
					clear();
					for (const auto& e : lt)
					{
						push_back(e);
					}
				}*/
				return *this;
			}

		private:
			Node* _head;//一个指链表的头指针
		};

		void test_List1()
		{


			List<int> lt;
			lt.push_back(1);
			lt.push_back(1);
			lt.push_back(1);
			lt.push_back(1);
			lt.push_front(3);
			for (auto e : lt)
			{
				cout << e;
			}
			cout << endl;

			List<int> lt2;

			lt2.push_back(1);
			lt2.push_back(2);
			lt2.push_back(3);
			lt2.push_back(4);
			lt2.push_front(5);

			for (auto e : lt2)
			{
				cout << e;
			}
			cout << endl;

			lt = lt2;

			for (auto e : lt)
			{
				cout << e;
			}
			cout << endl;
			/*List<int>::iterator it = lt.end();
			while (it != lt.begin())
			{
				cout << *it;
				it--;
			}*/

			//for (auto e : lt)
			//{
			//	cout << e;
			//}
			//cout << endl;
			//lt.clear();
			


		}

}
相关推荐
_日拱一卒9 分钟前
LeetCode:矩阵置零
java·数据结构·线性代数·算法·leetcode·职场和发展·矩阵
汀、人工智能1 小时前
[特殊字符] 第94课:删除无效的括号
数据结构·算法·数据库架构·图论·bfs·删除无效的括号
Imxyk1 小时前
P9244 [蓝桥杯 2023 省 B] 子串简写
数据结构·c++·算法
汀、人工智能1 小时前
[特殊字符] 第106课:旋转图像
数据结构·算法·矩阵·数据库架构·数组·旋转图像
牧瀬クリスだ1 小时前
七大排序一次满足
数据结构·算法·排序算法
liu****1 小时前
第15届省赛蓝桥杯大赛C/C++大学B组
开发语言·数据结构·c++·算法·蓝桥杯·acm
charlie1145141912 小时前
嵌入式Linux模块开发——struct module 深度解析:内核模块的核心数据结构
linux·开发语言·数据结构·c
计算机安禾2 小时前
【数据结构与算法】第37篇:图论(一):图的存储结构(邻接矩阵与邻接表)
数据结构·算法·链表·排序算法·深度优先·图论·visual studio code
sparEE2 小时前
基础排序算法:冒泡、选择、插入、希尔
数据结构·算法·排序算法
不爱吃炸鸡柳2 小时前
5道经典贪心算法题详解:从入门到进阶
开发语言·数据结构·c++·算法·贪心算法