C++之list模拟实现

发布于:2024-04-29 ⋅ 阅读:(29) ⋅ 点赞:(0)

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、源码


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