list的模拟实现(看看祖师爷如何实现迭代器)

发布时间:2024年01月14日
#pragma once

#include<assert.h>

namespace lhw
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& data=T())
			:_data(data)
			,_next(nullptr)
			,_prev(nullptr)
		{}
	};

	template<class T,class Ref,class Ptr>
	struct list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T, Ref, Ptr> self;
		node* _pnode;

		list_iterator(node* pnode)
			:_node(node)
		{}

		self& operator++()             //前置++
		{
			_pnode = _pnode->_next;
			return *this;
		}

		self operator++(int)          //后置++
		{
			self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}

		self& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		self operator--(int)
		{
			self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}

		bool operator==(const self& s)
		{
			return _pnode == s._pnode;
		}

		bool operator!=(const self& s)
		{
			return !(_pnode == s._pnode);
		}

		Ref operator*()
		{
			return _pnode->_data;
		}

		Ptr operator->()
		{
			return &_pnode->_data;       //获取当前对象_data的地址
		}
	};

	template<class T>
	class list
	{
		typedef list_node<T> node;
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
	public:
		list()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list(const list<T>& lt)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}

		list<T>& operator=(const list<T>& lt)
		{
			if (this != &lt)
			{
				clear();
				for (auto& e : lt)
				{
					push_back(e);
				}
			}
			return *this;
		}

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

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

		iterator begin()
		{
			return iterator(_head->_next);
		}

		const_iterator begin()const 
		{
			return const_iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);

		}

		const_iterator end()const
		{
			return const_iterator(_head);
		}

		T& front()
		{
			return _head->_next->_data;
		}

		const T& front()const
		{
			return _head->_next->_data;
		}
		

		T& back()
		{
			return _head->_prev->_data;
		}

		const T& back()const 
		{
			return _head->_prev->_data;
		}

		void insert(iterator pos, const T& val)
		{
			assert(pos._pnode);
			node* cur = pos._pnode;
			node* prev = cur->_prev;
			
			node* newnode = new node(val);
			newnode->_next = cur;
			newnode->_prev = prev;

			cur->_prev = newnode;
			prev->_next = newnode;

		}

		iterator erase(iterator pos)
		{
			assert(pos._pnode);
			node* cur = pos._pnode;
			node* next = cur->_next;
			node* prev = cur->_prev;

			delete cur;
			cur = nullptr;
			prev->_next = next;
			next->_prev = prev;

			return iterator(next);
		}

		void push_back(const T& val)
		{
			insert(iterator(_head), val);
		}

		void pop_back()
		{
			erase(iterator(_head->_prev));
		}

		void push_front(const T& val)
		{
			insert(iterator(_head->_next));
		}

		void pop_front()
		{
			erase(iterator(_head->_next));
		}

		size_t size()const
		{
			size_t sz = 0;
			iterator it = begin();
			while (it != end())
			{
				++sz;
				++it;
			}
			return sz;
		}

		void resize(size_t n, T& val)
		{
			size_t len = 0;
			iterator it = begin();
			while (it != end() && len < n) //1.迭代器遍历完(增加数据) 2.迭代器未遍历完成(删除数据)3.同时结束
			{
				len++;
				it++;
			}
			if (len == n)                  //说明此时容器的有效个数必定大于等于n
			{
				while (it != end())        //如果同时结束不执行
				{
					it = erase(it);
				}
			}
			else                           //增加数据
			{
				while (len < n)
				{
					push_back(val);
					len++;
				}
			}
		}

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

		bool empty()
		{
			return _head->_next == _head;
		}

		void swap(list<T>& lt)
		{
			::swap(_head, lt._head);
		}

	private:
		node* _head;
	};

}

文章来源:https://blog.csdn.net/mority_/article/details/135558058
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。