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

在這裏插入圖片描述

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