简单模拟STL list

STL list


list 底层维护了一个带头双向循环链表, list的迭代器不像vector、string 的迭代器是原生指针,我们通过将其抽象化,将迭代器封装成一个类,屏蔽其底层实现差异,提供统一的访问方式,使其使用起来像一个指针。


#include <iostream>
#include <algorithm>
#include <assert.h>



namespace my
{


	template<class T>
	struct __list_node
	{
	public:
		typedef __list_node<T> Node;

		__list_node(const T& x = T()) :
			_prev(nullptr),
			_next(nullptr),
			_data(x)
		{

		}

		Node* _prev;
		Node* _next;
		T _data;
	};


	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef __list_iterator<T, Ref, Ptr> self;
		typedef __list_node<T> Node;

		Node* _node;

		__list_iterator(Node* node) :
			_node(node)
		{

		}

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

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

		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		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;
		}

		bool operator!=(const self& it)
		{
			return _node != it._node;
		}
		bool operator==(const self& it)
		{
			return _node == it._node;
		}
	};

	// ---------------list 

	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);
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

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

		list()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}
		list(const list<T>& li) :
			_head(new Node(T()))
		{
			_head->_next = _head;
			_head->_prev = _head;

			const_iterator it = li.begin();
			while (it != li.end())
			{
				push_back(*it);
				++it;
			}
		}

		~list()
		{
			clear();

			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			auto it = begin();

			while (it != end())
			{
				it = erase(it);
			}

		}
		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 iterator(newNode);
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* next = pos._node->_next;
			Node* prev = pos._node->_prev;
			delete pos._node;

			prev->_next = next;
			next->_prev = prev;

			return iterator(next);
		}
		bool empty() const
		{
			return begin() == end();
		}
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			assert(!empty());
			erase(begin());
		}
		void pop_back()
		{
			assert(!empty());
			/*iterator end = end();
			iterator tail = --end;*/
			erase(--end());
		}

		void swap(list<T>& li)
		{
			std::swap(li._head, _head);
		}
		void reverse()
		{
			if (empty())
				return;

			Node* head = _head->_next;
			Node* tail = _head->_prev;

			while (head->_prev != tail && head != tail)
			{

				T tmp = head->_data;
				head->_data = tail->_data;
				tail->_data = tmp;

				head = head->_next;
				tail = tail->_prev;
			}

		}
		void print()
		{
			if (empty())
			{
				std::cout << "null" << std::endl;
				return;
			}

			iterator it = begin();
			while (it != end())
			{
				std::cout << *it << " ";
				it++;
			}
			std::cout << std::endl;
		}


	private:
		Node* _head;
	};

}

猜你喜欢

转载自blog.csdn.net/juggte/article/details/121268937