c++中list的简单实现-总结

时间:2024-03-10 15:49:40

可以先尝试一下 自己实现
代码总体加我自己的注释给在这里
实现完可以自己对比一下

template<class T>
//链表节点类
struct listNode
{
	listNode<T>* _next;
	listNode<T>* _prev;
	T date;
	
	listNode(const T& x = T())
		:_next(nullptr)
		,_prev(nullptr)
		,date(x)
	{}
};
	//正向迭代器类
	//Ref 来区别const和普通
	template<class T,class Ref,class Ptr>
	struct __list__iterator
	{
		typedef listNode<T> Node;
		typedef __list__iterator<T,Ref> self;
		Node* _node;
		__list__iterator(Node* node)
			:_node(node)
		{}
		//++it
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//it++
		self& operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node= _node->_prev;
			return *this;

		}
		self& operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		Ref operator*()
		{
			return _node->date;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
		Ptr operator->()
		{
			return &_node->date;
		}
	};
template<class T>
class list
{
	typedef listNode<T> Node;
public:
	typedef __list__iterator<T,T&,T*> iterator;
	typedef __list__iterator<T,const T&,const T*> const_iterator;
	typedef Reverselterator<T, T&, T*> reverse_iterator;
	const_iterator begin() const
	{
		return _head->_next;
	}
	const_iterator end() const
	{
		return _head;
	}
	iterator begin()
	{
		//return iterator(_head->_next);
		return _head->_next;
	}
	iterator end()
	{
		//return iterator(_head);
		return _head;
	}
	iterator rbegin()
	{
		return end();
	}
	iterator rend()
	{
		return begin();
	}
	void empty_init()//初始化节点
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}
	list()
	{
		empty_init();
	}
	list(list<T>& lt)
	{
		empty_init();
		for (const auto& ch : lt)
		{
			push_back(ch);
		}
	}
	~list()
	{
		clear();
		delete _head;
	}
	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}
	void swap(list<T>& lt)
	{
		std::swap(_head, lt._head);
	}
	list<T>& operator=(list<T> lt)
	{
		swap(lt);
		return *this;
	}
	void push_back(const T& x)//尾插
	{
		/*Node* newnode = new Node(x);
		Node* tail = _head->_prev;//链表尾部节点
		tail->_next = newnode;
		newnode->_prev = tail;
		newnode->_next = _head;
		_head->_prev = newnode;*/
		insert(end(), x);
	}
	void push_front(const T& x)//头插
	{
		insert(begin(), x);
	}
	void pop_back()//尾删
	{
		erase(--end());
	}
	void pop_front()//头删
	{
		erase(begin());
	}
	iterator 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;
		return newnode;
	}
	iterator erase(iterator pos)
	{
		assert(pos !=end());
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;
		prev->_next = next;
		next->_prev = prev;

		delete cur;
		return next;
	}
private:
	Node* _head;//哨兵位
};