【STL】之List的模拟实现

#include
using namespace std;
#include

namespace myself
{
	template
	struct ListNode
	{
		ListNode(const T& data = T())
			:_pPre(nullptr)
			, _pNext(nullptr)
			, _data(data)
		{ }
		ListNode* _pPre;
		ListNode* _pNext;
		T _data;
	};


	//list的迭代器:将指针封装(指针的使用:operator*()/->()/++/--/!=/==等...)
	template  // T 类型ref 引用 ptr 指针
	class ListIterator   
	{
		typedef ListNode Node;
		typedef Node* pNode;
		typedef ListIterator self;
	public:
		ListIterator(pNode pNode = nullptr)   //构造
			:_pNode(pNode)
		{ }
		ListIterator(const self& l)    //拷贝构造
			:_pNode(l._pNode)
		{ }
		T& operator*()
		{
			return _pNode->_data;
		}
		T* operator->()
		{
			return &(operator*());
		}
		self& operator++()
		{
			_pNode = _pNode->_pNext;
			return *this;
		}

		self operator++(int)
		{
			self temp(*this);
			_pNode = _pNode->_pNext;
			return temp;
		}
		self& operator--()
		{
			_pNode = _pNode->_pPre;
			return *this;
		}

		self operator--(int)
		{
			self temp(*this);
			_pNode = _pNode->_pPre;
			return temp;
		}
		bool operator!=(const self& l)
		{
			return _pNode != l._pNode;
		}

		bool operator==(const self& l)
		{
			return _pNode != l._pNode;
		}
		
        pNode _pNode;
	};

	/*
	List 的反向迭代器,反向迭代器与正向迭代器刚好是相反的,反向迭代器++,迭代器往前移动,反向迭代
	器--, 迭代器往后移动,因此反向迭代器可以在正向迭代器的基础之上来实现
	*/
	template
	class ListReverseIterator
	{
		typedef ListReverseIterator self;
	public:
		ListReverseIterator(const iterator& it)
			: _it(it)
		{ }
			ListReverseIterator(const self& l)
			: _it(l._it)
		{ }
		Ref operator*()
		{
			iterator temp = _it;
			return *(--temp);
		}
		Ptr operator->()
		{
			return &operator*();
		}
		bool operator!=(const self& l)
		{
			return _it != l._it;
		}
		bool operator==(const self& l)
		{
			return _it != l._it;
		}
	private:
		iterator _it;
	};

    template
	class list
	{
		typedef ListNode Node;
		typedef Node* pNode;
	public:
		typedef ListIterator iterator;
		typedef ListIterator Constiterator;
		typedef ListReverseIterator ReverseIterator;

	public:
        //------------------------- List 构造--------------------------------// 
		list()   //空链表(仍有头结点)
		{ 
			CreatHead();
		}
		list(int n, const T& data = T())  //构造函数
		{
			CreatHead();
			for (int i = 0; i < n; ++i)
				push_back(data);
		}

		
		template 
		list(ListIterator first, ListIterator last)  //构造函数
		{
			CreatHead();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		
		
		//list(T* first, T* last)  //构造函数
		//{
		//	CreatHead();
		//	while (first != last)
		//		push_back(*first++);
		//}
		
		list(const list& l)     //拷贝构造
		{
			CreatHead();
			list tmp(l.Cbegin(), l.Cend());   //构造临时tmp然后交换
			this->Swap(tmp);
		}
		
		list& operator=(const list& l)             //赋值运算符重载
		{
			if(this != &l)
			{
				list tmp(l);
				this->Swap(tmp);
			}
			return *this;
		}
		
		
		~list()  //析构函数
		{
			clear();
			delete _pHead;
			_pHead = nullptr;
		}

        //------------------------- List Capacity--------------------------------//  
		size_t size()const     //获取list中有效元素个数
		{
			size_t count = 0;
			pNode pCur = _pHead->_pNext;
			while(pCur != _pHead)
			{
				count++;
				pCur = pCur->_pNext;
			}
			return count;
		}
		void resize(size_t newSize, const T& data)   //将list中有效元素的个数改为n个,多出的元素用data填充
		{
			size_t oldSize = size();
			if (oldSize < newSize)
			{
				for(size_t i = oldSize; i < newSize; ++i)
					push_back(data);
			}
			else 
			{
				for(size_t j = newSize;j < oldSize; ++j)
					pop_back();
			}

		}
		bool empty()const  //判空
		{
			return _pHead->_pNext == _pHead;
		}


       //------------------------- List Modify--------------------------------//  
		
		void push_back(const T& data)  //尾插
		{
			pNode pNewNode = new Node(data);
			pNewNode->_pNext = _pHead;
			pNewNode->_pPre = _pHead->_pPre;
			pNewNode->_pPre->_pNext = pNewNode;
			_pHead->_pPre = pNewNode;
		}
		void pop_back()   //尾删   
		{
			pNode pDel = _pHead->_pPre;
			if (pDel != _pHead)
			{
				pDel->_pPre->_pNext = _pHead;
				_pHead->_pPre = pDel->_pPre;
				delete pDel;
			}
		}
		void push_front(const T& data)   //头插   
		{
			pNode pNewNode = new Node(data);
			pNewNode->_pPre = _pHead;
			pNewNode->_pNext = _pHead->_pNext;
			pNewNode->_pNext->_pPre = pNewNode;
			_pHead->_pNext = pNewNode;

		}
		void pop_front()  //头删   
		{
			pNode pDel = _pHead->_pNext;
			if(pDel != _pHead)
			{
				pDel->_pNext->_pPre = _pHead;
				_pHead->_pNext = pDel->_pNext;
				delete pDel;
			}
		}
		iterator insert(iterator pos, const T& data)              // 在pos位置前插入值为data的节点
		{
			pNode pNewNode = new Node(data);
			pNode pCur = pos._pNode;
			pNewNode->_pNext = pCur;
			pNewNode->_pPre = pCur->_pPre;
			pCur->_pPre->_pNext = pNewNode;
			pCur->_pPre = pNewNode;
			return iterator(pNewNode);
		}
		iterator erase(iterator pos)      // 删除pos位置的节点,返回该节点的下一个位置
		{
			pNode pDel = pos._pNode;  //待删除节点
			pNode pRet = pDel->_pNext; //返回的节点
			if (pDel == _pHead)
				return pos;
			
			//删除节点
			pDel->_pPre->_pNext = pDel->_pNext;
			pDel->_pNext->_pPre = pDel->_pPre;
			delete pDel;

			return pRet;

		}
		void clear()   //清空
		{
			pNode pCur = _pHead->_pNext;
			while(pCur != _pHead)
			{
				_pHead->_pNext = pCur->_pNext;
				delete pCur;
				pCur = _pHead->_pNext;
			}
			_pHead->_pNext = _pHead;
			_pHead->_pPre = _pHead;
		}
		void Swap(list& l)  //交换
		{
			swap(_pHead, l._pHead);
		}
       //------------------------- List Iterator--------------------------------//  		
		iterator begin()
		{
			return iterator(_pHead->_pNext);
		}
		iterator end()
		{
			return iterator(_pHead);
		}
		Constiterator Cbegin() const
		{
			return Constiterator(_pHead->_pNext);
		}
		Constiterator Cend() const
		{
			return Constiterator(_pHead);
		}

        //------------------------- List Access--------------------------------//  		
		T& front()
		{
			return _pHead->_pNext->_data;
		}
		const T& front()const 
		{
			return _pHead->_pNext->_data;
		}
		T& back()
		{
			return _pHead->_pPre->_data;
		}
		const T& back()const 
		{
			return _pHead->_pPre->_data;
		}

	private:
		void CreatHead()
		{
			_pHead = new Node;  //头结点内不放数据
			_pHead->_pNext = _pHead;
			_pHead->_pPre = _pHead;
		}
	private:
		pNode _pHead;
	};
}




-------------------------------------test---------------------------------------

// 正向打印链表
template
void PrintList(myself::list& l)
{
	auto it = l.begin();
	while(it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}


void TestList1()
{
	myself::list l1;
	myself::list l2(10,5);
	PrintList(l2);
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	myself::list l3(array, array + sizeof(array) / sizeof(array[0]));
	PrintList(l3);
	myself::list l4(l3);
	PrintList(l4);
	l1 = l4;
	PrintList(l1);
} 

void TestList2()
{
	myself::list l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	PrintList(l);

	l.pop_back();	
	PrintList(l);
	cout << l.size() << endl;

	l.push_front(2);
	l.push_front(3);
	PrintList(l);

	l.pop_front();
	l.pop_front();
	PrintList(l);
	cout << l.size() << endl;


}
void TestList3()
{
	int array[] = { 1, 2, 3, 4, 5 };
	myself::list l(array, array + sizeof(array) / sizeof(array[0]));
	auto pos = l.begin();
	l.insert(l.begin(), 0);
	PrintList(l);

	++pos;
	l.insert(pos, 8);
	PrintList(l);
	
	l.erase(l.begin());
	l.erase(pos);
	PrintList(l);// pos指向的节点已经被删除,pos迭代器失效
	cout << *pos << endl; 

	auto it = l.begin();
	while (it != l.end())
	{
		it = l.erase(it);
	}
	cout << l.size() << endl;
	
}
void TestList4()
{
	int array[] = { 1, 2, 3, 4, 5 };
	myself::list l(array, array + sizeof(array) / sizeof(array[0]));
	cout << l.size() << endl;
	l.resize(10, 6);
	PrintList(l);

	myself::list ll(10, 5);
	ll.Swap(l);
	PrintList(l);
	PrintList(ll);

	l.clear();
	cout << l.size() << endl;
	PrintList(l);





}
int main()
{
	//TestList1();
	//TestList2();
	//TestList3();
	TestList4();
	system("pause");
	return 0;
}

 

你可能感兴趣的:(【STL】之List的模拟实现)