【C++】list


描述

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

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

3.list与forward_list非常相似:主要不同在于forward_list是单链表,只能单方向迭代。

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

5.与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置。

6.list还需要一些额外的空间,以保存每个节点的相关联信息。

一、list(STL库)

常见接口

在这里插入图片描述

list使用

链表头插、链表尾插
在这里插入图片描述

链表头删、链表尾删
在这里插入图片描述

链表遍历
在这里插入图片描述

链表指定位置的插入和删除
在这里插入图片描述

clear清除
在这里插入图片描述

构造函数
在这里插入图片描述

二、list 模拟实现

1.节点

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

2.构造函数,析构函数,赋值运算符重载,clear函数

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 swap(list<T>& tmp)
{
    
    
	//std标准库中的swap函数
	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;
}
//析构函数
~list()
{
    
    
	clear();
	delete _head;
	_head = nullptr;
	
}
//清除函数,用来清除链表数据,保留头结点
void clear()
{
    
    
	iterator it = begin();
	while (it != end())
	{
    
    
		it = erase(it);
	}
}

3.迭代器(iterator)

list的迭代器与vector和string不同,list的迭代器不是原生指针,而是对链表结点指针的封装,实现与vector和string功能相同的迭代器,list迭代器本质上是链表结点的指针,链表结点的指针不能进行++操作,因为链表结构的特殊,存储的地址不连续,我们使用封装和运算符重载实现了可以对迭代器进行的各种++操作,以及比较,解引用等操作。

//迭代器的三个类模板参数,第一个是迭代器指向内容的数据类型,
//第二个参数是为了可以同时实现普通迭代器和const迭代器,使用模板参数,减少写冗余代码
//第三个参数是重载->操作符,为了方便迭代器指向的元素类型是自定义类型,可以使用迭代器访问其成员变量
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;
	}
};

3.begin(),end()

begin()返回一个指向收个元素的迭代器
end()返回的迭代器指向最后一个元素的后一个位置,最后一个元素的后一个位置为头结点,所以end()返回指向头结点的迭代器

在这里插入图片描述

typedef _list_iterator<T,T&,T*> iterator;
typedef _list_iterator<T,const T&,const T*> const_iterator;
//这里使用typedef更方便使用实现的迭代器,
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);
}

4.头插、尾插、指定位置插入

void push_back(const T& x)
{
    
    
	//可以复用insert    
	//insert(end(), x);
	node* tail = _head->_prev;
	node* new_node = new node(x);

	tail->_next = new_node;
	new_node->_prev = tail;

	new_node->_next = _head;
	_head->_prev = new_node;
}
//头插
void push_front(const T& x)
{
    
    
	//复用insert 头插即为begin()位置插入元素
	insert(begin(), x);
}
//指定位置插入
void insert(iterator pos,const T& x)
{
    
    
	node* cur = pos._node;
	node* prev = cur->_prev;

	node* new_node = new node(x);

	prev->_next = new_node;
	new_node->_prev = prev;
	new_node->_next = cur;
	cur->_prev = new_node;		 
}

5.头删、尾删、指定位置删

//pos位置删除元素
iterator erase(iterator pos)
{
    
    
	assert(pos != end());
	node* prev = pos._node->_prev;
	node* next = pos._node->_next;

	prev->_next = next;
	next->_prev = prev;
	delete pos._node;

	return iterator(next);
}
//尾删   复用erase
void pop_back()
{
    
    
	erase(--end());
}
//头删   复用erase
void pop_front()
{
    
    
	erase(begin());
}

三、模拟实现源码

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<algorithm>


namespace tzc {
    
    

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


	};


	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;

		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);
		}
		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>& lt)
		//{
    
    
		//	empty_init();
		//	for (auto e : lt)
		//	{
    
    
		//		push_back(e);
		//	}
		//}
		void swap(list<T>& tmp)
		{
    
    
			//std标准库中的swap函数
			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;
		}

		~list()
		{
    
    
			clear();
			delete _head;
			_head = nullptr;
			
		}
		void clear()
		{
    
    
			iterator it = begin();
			while (it != end())
			{
    
    
				it = erase(it);
			}
		}
		void push_back(const T& x)
		{
    
    
			//可以复用insert    
			//insert(end(), x);
			node* tail = _head->_prev;
			node* new_node = new node(x);

			tail->_next = new_node;
			new_node->_prev = tail;

			new_node->_next = _head;
			_head->_prev = new_node;
		}
		void push_front(const T& x)
		{
    
    
			insert(begin(), x);
		}

		void insert(iterator pos,const T& x)
		{
    
    
			node* cur = pos._node;
			node* prev = cur->_prev;

			node* new_node = new node(x);

			prev->_next = new_node;
			new_node->_prev = prev;
			new_node->_next = cur;
			cur->_prev = new_node;
				 
		}

		iterator erase(iterator pos)
		{
    
    
			assert(pos != end());
			node* prev = pos._node->_prev;
			node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			delete pos._node;

			return iterator(next);
		}

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

	private:
		node* _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 test_list1()
	{
    
    
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

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

	void test_list2()
	{
    
    
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

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

		auto pos = lt.begin();
		++pos;
		lt.insert(pos, 20);
		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;


		lt.push_back(5);
		lt.push_front(100);
		lt.pop_back();
		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;	
	}

	void test_list3()
	{
    
    
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		lt1.clear();
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list4()
	{
    
    
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		list<int> lt2(lt1);
		for (auto e : lt2)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		list<int> lt3;
		lt3 = lt2;
		for (auto e : lt3)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list5()
	{
    
    
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		list<int>::iterator it = lt1.begin();
		lt1.insert(it, 20);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		it = lt1.begin();
		lt1.erase(it);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list6()
	{
    
    
		list<int>list1, list2;
		for (int i = 1; i <= 4; i++)
		{
    
     //list1  1 2 3 4
			list1.push_back(i);
		}
		for (int i = 1; i <=3; i++)
		{
    
      //list2  10   20   30
			list2.push_back(i*10);
		}

		for (auto e : list1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
		for (auto e : list2)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/Tianzhenchuan/article/details/131750579