List的模拟实现(C++)

#include <iostream>
#include <stdlib.h>

using namespace std;

template <class T>
struct ListNode
{
	ListNode(const T& val = T())
	: _data(val)
	, _next(nullptr)
	, _prev(nullptr)
	{}

	T _data;
	ListNode<T>* _next;
	ListNode<T>* _prev;
};

template <class T, class Ref, class Ptr>
struct ListIterator
{
	typedef ListIterator <T, Ref, Ptr> Self;
	ListNode<T>* _node;
	ListIterator(ListNode<T>* node)
		: _node(node)
	{}

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

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

	Self& operator++()
	{
		_node = _node->_next;
		return *this;
	}

	Self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}

	Self operator++(int)
	{
		Self tmp(*this);
		_node = _node->_next;
		return tmp;
	}

	bool operator !=(const Self& lit)
	{
		return _node != lit._node;
	}

	bool operator==(const Self& lit)
	{
		return _node == lit._node;
	}
};

template <class T>
class List
{
public:
	typedef ListNode<T> Node;
	typedef ListIterator<T, T&, T*> iterator;
	typedef const ListIterator<T, T&, T*> const_iterator;
	typedef Node* pNode;

	List()
	{
		cout << "Constructor called!" << endl;
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}


	//拷贝构造函数,要实现深拷贝
	List(const List& lst)
	{
		//先创建头节点
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;

		//再将数据赋进去
		for (const auto &e : lst)
		{
			PushBack(e);
		}
	}

	~List()
	{
		cout << "destructor called!" << endl;
		Clear();
		if (_head)
		{
			delete _head;
			_head = nullptr;
		}
	}

	//赋值运算符重载
	List<T>& operator=(const List<T> lst)
	{
		if (this != &lst)
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			for (const auto& e : lst)
			{
				PushBack(e);
			}
		}
		return *this;
	}

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

	

	//尾插
	void PushBack(const T& val)
	{
		pNode newNode = new Node(val);
		newNode->_next = _head;
		newNode->_prev = _head->_prev;
		newNode->_prev->_next = newNode;
		_head->_prev = newNode;
	}

	//尾删
	void PopBack()
	{
		//先找到最后一个节点
		pNode pDel = _head->_prev;
		if (pDel != _head)
		{
			pDel->_prev->_next = _head;
			_head->_prev = pDel->_prev;
			delete pDel;
		}
		else
			return;
	}

	//头插
	void PushFront(const T& val)
	{
		//若还没有有效节点,则就是尾插
		if (isEmpty())
		{
			PushBack(val);
		}
		else
		{
			pNode newNode = new Node(val);
			_head->_next->_prev = newNode;
			newNode->_next = _head->_next;
			_head->_next = newNode;
			newNode->_prev = _head;
		}
	}

	//头删
	void PopFront()
	{
		//若没有有效节点
		if (isEmpty())
		{
			cout << "删除失败!" << endl;
		}
		else
		{
			//找到要删除的节点
			pNode pDel = _head->_next;
			_head->_next = pDel->_next;
			pDel->_next->_prev = pDel->_prev;
			delete pDel;
		}
	}

	//在pos位置前插入节点
	void Insert(iterator pos, const T& val)
	{
		pNode newNode = new Node(val);
		pNode cur = pos._node;
		newNode->_prev = cur->_prev;
		newNode->_prev->_next = newNode;
		newNode->_next = cur;
		cur->_prev = newNode;
	}

	//删除pos位置的节点,返回该节点的下一个位置
	iterator Erase(iterator pos)
	{
		//不能删除头结点
		if (pos != end())
		{
			pNode cur = pos._node;
			cur->_prev->_next = cur->_next;
			cur->_next->_prev = cur->_prev;
			//更新迭代器
			pos = iterator(cur->_next);
			delete cur;
		}
		return pos;
	}




	void Clear()
	{
		//清空,即保留头节点,删除其他所有有效节点

			pNode cur = _head->_next;
			while (cur != _head)
			{
				_head->_next = cur->_next;
				delete cur;
				cur = _head->_next;
			}
		
		_head->_next = _head;
		_head->_prev = _head;
	}

	//获取节点个数
	size_t Size() const
	{
		size_t count = 0;
		pNode cur = _head->_next;
		while (cur != _head)
		{
			++count;
			cur = cur->_next;
		}
		return count;
	}


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

	void printList()
	{
		auto lit = this->begin();
		while (lit != this->end())
		{
			cout << *lit << " ";
			++lit;
		}
		cout << endl;
	}

private:
	pNode _head;
};


void Test()
{
	List<int> list;
	list.PushBack(1);
	list.PushBack(2);
	list.PushBack(3);
	list.PushBack(4);
	list.PushBack(5);
	cout << "PushBack:";
	list.printList(); //1 2 3 4 5
	cout << "PopBack:";
	list.PopBack();
	list.printList();// 1 2 3 4
	cout << "PushFront:";
	list.PushFront(0);
	list.printList();// 0 1 2 3 4
	cout << "PopFront:";
	list.PopFront();
	list.printList();//  1 2 3 4
	cout << "Insert at pos:";
	auto pos = list.begin();
	list.Insert(pos, 0);
	list.printList();// 0 1 2 3 4
	cout << "++pos:";
	++pos;
	list.Insert(pos, 3);
	list.printList();// 0  1 3 2 3 4
	cout << "Erase at pos:";
	list.Erase(pos);
	list.printList(); //0 1 3 3 4
	cout << "Clear:";
	list.Clear();
	cout << "Size:" << list.Size() << endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章