C++之list模拟实现

1、定义

定义一个结点:

在list类中的定义:

2、push_back()

3、迭代器

3.1迭代器的构造和定义

3.2、迭代器中的取值

3.3、迭代器的迭代(前置++或前置--)

3.4、迭代器的迭代(后置++或后置--)

3.5、迭代器的判断

3.6、在类list的定义

4.begin()和end()

5.const begin()和const end()

6、构造函数

7、拷贝构造函数

8、赋值=

传统方式 :

现代写法:

9、析构函数

10、insert()

11、push_front()

12、pop_back()

13、pop_front()

14、erase()

15、源码

cpp 复制代码
namespace zzw
{

	template <class T>
	struct __list_node
	{
		__list_node<T>* _next;
		__list_node<T>* _prev;
		T  _date;

		__list_node(const T& x = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_date(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* node)
			:_node(node)
		{}

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

		self operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator++(int)
		{
			self temp(_node);
			//_node = _node->_next;
			++(*this);
			return temp;
		}
		self operator--(int)
		{
			self temp(_node);
			//_node = _node->prev;
			--(*this);
			return temp;
		}
		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;
		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);
		}

		list(const T& x = T())
		{
			_head = new Node(x);
			_head->_next = _head;
			_head->_prev = _head;
		}


		list(const list<T>& lt)
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

		/*	const_iterator it = lt.begin();
			while (it != lt.end())
			{
				push_back(*it);
				++it;
			}*/
			for (auto e : lt)
			{
				push_back(e);
			}

		}

	/*	list<T>& operator=(const list<T>& lt)
		{
			if (this != &lt)
			{
				clear();
				const_iterator it = lt.begin();
				while (it != lt.end())
				{
					push_back(*it);
					++it;
				}
			}
			
			return *this;
		}*/

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

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

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

		void push_back(const T& x = T())
		{
			Node* tail = _head->_prev;
			Node* New_node = new Node(x);


			tail->_next = New_node;
			New_node->_prev = tail;
			_head->_prev = New_node;
			New_node->_next = _head;

			/*insert(--end(), x);*/
		}

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

		void insert(const iterator& pos,const T& x = T())
		{
			Node* cur = pos._node;
			Node* next = cur->_next;
			Node* new_node = new Node(x);

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

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

		void  erase(const iterator& pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			
			delete cur;
			prev->_next = next;
			next->_prev = prev;
		
		}

	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_list()
	//{
	//	list<int> lt;
	//	lt.push_back(1);
	//	lt.push_back(2);
	//	lt.push_back(3);
	//	lt.push_back(4);

	//	print_list(lt);
	//}

	//struct Date
	//{
	//	int _year=0;
	//	int _month=1;
	//	int _day=1;
	//};

	//void test_list2()
	//{
	//	list<Date> lt;
	//	lt.push_back(Date());
	//	lt.push_back(Date());

	//	list<Date>::iterator it = lt.begin();

	//	while (it!=lt.end())
	//	{
	//		cout << (*it)._year << "-" << (*it)._month << "-" << (*it)._day << endl;
	//		cout << it->_year << "-" << it->_month << "-" << it->_day << endl;
	//		++it;
	//	}
	//	cout << endl;
	//}

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

		list<int> lt2(lt);
		list<int> lt3;
		lt2 = lt3;
		print_list(lt2);
		print_list(lt3);
		print_list(lt);
	}
}; 
相关推荐
How_doyou_do22 分钟前
数据传输优化-异步不阻塞处理增强首屏体验
开发语言·前端·javascript
jingfeng51439 分钟前
C++11可变参数模板、emplace系列接口、包装器
开发语言·c++
云天徽上40 分钟前
【数据可视化-107】2025年1-7月全国出口总额Top 10省市数据分析:用Python和Pyecharts打造炫酷可视化大屏
开发语言·python·信息可视化·数据挖掘·数据分析·pyecharts
Tina表姐1 小时前
(C题|NIPT 的时点选择与胎儿的异常判定)2025年高教杯全国大学生数学建模国赛解题思路|完整代码论文集合
c语言·开发语言·数学建模
Kevinhbr1 小时前
CSP-J/S IS COMING
数据结构·c++·算法
蕓晨1 小时前
set的插入和pair的用法
c++·算法
金古圣人2 小时前
hot100 滑动窗口
数据结构·c++·算法·leetcode·哈希算法
蒹葭玉树2 小时前
【C++上岸】C++常见面试题目--算法篇(第二十期)
c++·算法·面试
轮到我狗叫了2 小时前
牛客.小红的子串牛客.kotori和抽卡牛客.循环汉诺塔牛客.ruby和薯条
java·开发语言·算法
yudiandian20142 小时前
【QT 5.12.12 下载 Windows 版本】
开发语言·qt