STL---list实现(C++)

#pragma once
#include<assert.h>
//#include<iostream>
//using std::cout;
//using std::cin;
//using std::endl;
namespace bit
{
	template<class T>
	struct __list_node
	{
		__list_node<T>* _next;
		__list_node<T>* _prev;
		T _data;
		__list_node(const T& x = T())
			:_data(x)
			, _next(nullptr)
			, _prev(nullptr)
		{
		}
	};

	//__list_iterator<T,T&,T*> ->iterator
	//__list_iterator<T,const T&,const T*> ->const_iterator
	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)
		{}

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

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

		//it++
		Self operator++(int)
		{
			Self temp(*this);
			_node = _node->_next;
			return temp;
		}

		//--it
		Self& operator--() {
			_node = _node->_prev;
			return *this;
		}

		//it--
		Self operator--(int)
		{
			Self temp(*this);
			_node = _node->_prev;
			return temp;
		}

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

		//it == end()
		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()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		//拷贝构造函数
		list(const list<T>& It) {
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			for(auto e:It)
				push_back(e)
			/*const_iterator it = It.begin();
			while (it != It.end()) {
				this->push_back(*it);
				++it;
			}*/
		}
		list<T>& operator=(const list<T>& It) {
			if (this != &It)
			{
				clear();
				for (auto e : It)
					push_back(e);
			}
			return *this;
		}
		//现代写法:
		/*list<T>& operator=(list<T>& It)
		{
			swap(_head, It._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)
		{
			insert(end(), x);
		}

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

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

		void pop_front() {
			erase(begin());
		}
		void 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;
		}
		void erase(iterator pos) {
			assert(pos != end());//不可以删除头结点
			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>& a) {
		list<int>::const_iterator it = a.begin();
		while (it != a.end()) {
			cout << (*it) << " ";
			++it;
		}
		cout << endl;
	}
	void test_list1()
	{
		list<int> a;
		a.push_back(1);
		a.push_back(2);
		a.push_back(3);
		a.push_back(4);
		list<int> b(a);
		print_list(b);
	}
	//void test_list1()
	//{
	//	list<int> a;
	//	a.push_back(1);
	//	a.push_back(2);
	//	a.push_back(3);
	//	a.push_back(4);

	//	list<int>::iterator it = a.begin();
	//	while (it != a.end()) {
	//		cout << *it << " ";
	//		++it;
	//	}
	//	cout << endl;
	//}
	//struct Date
	//{
	//	int _year = 0;
	//	int _month = 1;
	//	int _day = 1;
	//};

	//void test_list2() {
	//	list<Date> a;
	//	a.push_back(Date());
	//	//这里为什么用匿名对象啊啊啊啊??
	//	a.push_back(Date());
	//	list<Date>::iterator it = a.begin();
	//	while (it != a.end()) {
	//		cout << it->_year << "-" << it->_month << "-" << it->_day << endl;
	//		//cout << (*it)._year << "-" << (*it)._month << "-" << (*it)._day << endl;
	//		++it;
	//	}



	//}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值