List C/C++代码实现

template<typename Object>
class List
{
private:
	struct Node
	{
		Object data;
		Node *prev;
		Node *next;
		Node(const Object& d = {}, Node*p = nullptr, Node * n = nullptr) :data(d), prev(p), next(n) {}
		Node(Object && d, Node*p = nullptr, Node * n = nullptr) :data(move(d)), prev(p), next(n) {}
	};
private:
	int Size;
	Node * head;
	Node * tail;
public:
	class const_iterator
	{
	protected:
		Node * current;
		const List<Object>*theList;
		Object & retrieve()const { return current->data; }
		const_iterator(Node * p, const List<Object>&lst) :current(p), theList(&lst) {}
		friend class List<Object>;
	public:
		void assertIsValid() const
		{
			if (theList == nullptr || current == nullptr || current == theList->head)
				throw "iteratorOutOfBounds";

		}
		const_iterator() :current(nullptr) {}
		const Object & operator*()const { return retrieve(); }
		const_iterator & operator++() { current = current->next; return *this; }
		const_iterator operator++(int)
		{
			const_iterator old = *this;
			++(*this);
			return old;
		}
		const_iterator & operator--()
		{
			current = current->prev;
			return *this;
		}
		const_iterator & operator--(int)
		{
			const_iterator old = *this;
			--(*this);
			return old;
		}
		const_iterator & operator+(int k)
		{
			const_iterator advanced = *this;
			for (int i = 0; i < k; ++i)
			{
				advanced.current = advanced.current->next;
			}
			return advanced;
		}
		bool operator==(const const_iterator & rhs)const { return current == rhs.current; }
		bool operator!=(const const_iterator & rhs)const { return !(*this == rhs); }
	};
	class iterator :public const_iterator
	{
	public:
		iterator() { }
		Object&operator*()
		{
			return const_iterator::retrieve();
		}
		const Object&operator*()const
		{
			return const_iterator::operator*();
		}
		iterator & operator++()
		{
			this->current = this->current->next;
			return *this;
		}
		iterator & operator++(int)
		{
			iterator old = *this;
			++(*this);
			return old;
		}
		iterator & operator--()
		{
			this->current = this->current->prev;
			return *this;
		}
		iterator & operator+(int k)
		{
			iterator advanced = *this;
			for (int i = 0; i < k; ++i)
			{
				advanced.current = advanced.current->next;
			}
			return advanced;
		}
		bool operator==(const iterator & rhs)const { return this->current == rhs.current; }
		bool operator!=(const iterator & rhs)const { return !(*this == rhs); }

	protected:
		iterator(Node *p,List<Object>&lst) :const_iterator(p,lst) {}
		friend class List<Object>;
	};

	iterator begin() { return { head->next,*this }; }
	const_iterator begin() const { return { head->next,*this }; }
	iterator end() { return{ tail,*this }; }
	const_iterator end()const { return{ tail,*this }; }
public:
	List() :Size(0), head(new Node), tail(new Node) 
	{ head->next = tail; tail->prev = head; }
	List(const List & rhs) :Size(0), head(new Node), tail(new Node)
	{
		head->next = tail;
		tail->prev = head;
		for (auto & x : rhs)
			push_back(x);
	}
	List(List && rhs) :Size(rhs.Size), head(rhs.head), tail(rhs.tail)
	{
		rhs.Size = 0;
		rhs.head = nullptr;
		rhs.tail = nullptr;
	}
	List & operator=(const List&rhs)
	{
		List copy = rhs;
		swap(*this, copy);
		return *this;
	}
	List & operator=(List && rhs)
	{
		swap(Size, rhs.Size);
		swap(head, rhs.head);
		swap(tail, rhs.tail);
		return *this;
	}
	~List() { clear(); delete head; delete tail; }
public:
	int size()const { return Size; }
	bool empty()const { return Size == 0; }
	iterator erase(iterator itr)
	{
		itr.assertIsValid();
		if (itr.theList != this)
			throw "IteratorMismatch";
		Node * p = itr.current;
		iterator retVal{ p->next,*this };
		p->prev->next = p->next;
		p->next->prev = p->prev;
		delete p;
		--Size;
		return retVal;
	}
	iterator erase(iterator from, iterator to)
	{
		from.assertIsValid();
		if (from.theList != this || from.theList != to.theList)
			throw "IteratorMismatch";
		for (iterator itr = from; itr != to; )
			itr = erase(itr);
		return to;
	}
	iterator insert(iterator itr, const Object &x)
	{
		itr.assertIsValid();
		if (itr.theList != this)
			throw "IteratorMismatch";
		Node * p = itr.current;
		++Size;
		return { p->prev = p->prev->next = new Node{x,p->prev,p},*this };
	}
	iterator insert(iterator itr, Object && x)
	{
		itr.assertIsValid();
		if (itr.theList != this)
			throw "IteratorMismatch";
		Node * p = itr.current;
		++Size;
		return { p->prev = p->prev->next = new Node{move(x),p->prev,p},*this };
	}
	Object & front() { return *begin(); }
	Object & back() { return *--end(); }

	const Object&front()const { return*begin(); }
	const Object & back()const { return *--end(); }

	void push_front(const Object & x) { insert(begin(), x); }
	void push_front(const Object && x) {insert(begin(), move(x));}
	void push_back(const Object & x) { insert(end(), x); }
	void push_back(const Object && x) { insert(end(), move(x)); }
	void pop_front() { erase(begin()); }
	void pop_back() { erase(--end()); }

	void clear()
	{
		while (Size)
			pop_front();
	}
};

猜你喜欢

转载自blog.csdn.net/qq_44800780/article/details/104211395