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);
	}
}; 
相关推荐
threelab1 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师721 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
不知名的老吴1 小时前
线程的生命周期之线程“插队“
java·开发语言·python
Hello:CodeWorld2 小时前
C 风格变参 vs C++ 变参模板:核心区别与选型指南
c语言·c++·算法
kaikaile19952 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
秋93 小时前
Go语言(Golang)开发工程师全景解析:岗位职责·语言优势与使用场景·各城市薪资·发展前景·高考志愿填报(2026版)
开发语言·golang·高考
huangdong_4 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
搬砖魁首4 小时前
基础能力系列 - 多线程2 - 条件变量
c++·rust·条件变量·原子类型·线程同步互斥
chase_my_dream4 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
牛油果子哥q4 小时前
【C++ STL string 】C++ STL string 终极精讲:底层原理、内存机制、全套API、深浅拷贝、易错坑点与工程实战规范
数据库·c++