实现 STL--list

#pragma once
#include<iostream>


// 带头结点双向循环链表
template<class T>
struct ListNode
{
	ListNode(const T& data = T())
	: _pPre(0)
	, _pNext(0)
	, _data(data)
	{}

	ListNode<T>* _pPre;
	ListNode<T>* _pNext;
	T _data;
};


// 迭代器:指针
// List 的 迭代器
template<class T, class Ref, class Ptr>
struct ListIterator
{
	typedef ListNode<T> Node;
	typedef Node* PNode;
	typedef ListIterator<T, Ref, Ptr> Self;
public:
	ListIterator()
		: _pNode(0)
	{}

	ListIterator(PNode pNode)
		: _pNode(pNode)
	{}

	ListIterator(const Self& s)
		: _pNode(s._pNode)
	{}

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

	Ptr operator->()
	{
		return &(operator*());
	}

	Self& operator++()
	{
		_pNode = _pNode->_pNext;
		return *this;
	}

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

	bool operator!=(const Self& s)
	{
		return _pNode != s._pNode;
	}

	bool operator==(const Self& s)
	{
		return _pNode == s._pNode;
	}

private:
	PNode _pNode;
};

template<class T>
class List
{
	typedef ListNode<T> Node;
	typedef Node* PNode;
public:
	typedef ListIterator<T, T&, T*> Iterator;

public:
	List()
	{
		_CreateHead();
	}

	// size个值为data的结点
	List(size_t size, const T& data = T())
	{
		_CreateHead();
		for (size_t i = 0; i < size; ++i)
			PushBack(data);
	}

	// [first, last)
	List(const T* first, const T* last)
	{
		_CreateHead();
		while (first != last)
		{
			PushBack(*first);
			++first;
		}
	}

	List(const List<T>& l);
	List<T>& operator=(const List<T>& l);
	~List()
	{
		Clear();
		delete _pHead;
		_pHead = NULL;
	}

	////////////////////////////////////////////////////////
	// Iterator
	Iterator Begin()
	{
		return _pHead->_pNext;
	}

	Iterator End()
	{
		return _pHead;
	}
	////////////////////////////////////////////////////////
	// capacity
	bool Empty()const
	{
		return _pHead->_pNext == _pHead;
	}

	size_t Size()const
	{
		PNode pCur = _pHead->_pNext;
		size_t count = 0;
		while (pCur != _pHead)
		{
			count++;
			pCur = pCur->_pNext;
		}

		return count;
	}


	void Resize(size_t newSize, const T& data = T())
	{
		size_t oldSize = Size();
		if (newSize > oldSize)
		{
			for (size_t i = oldSize; i < newSize; ++i)
				PushBack(data);
		}
		else
		{
			for (size_t i = oldSize; i >= newSize; --i)
				PopBack();
		}
	}

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

	///////////////////////////////////////////////
	// modify
	void PushBack(const T& data)
	{
		PNode pNewNode = new Node(data);
		pNewNode->_pNext = _pHead;
		pNewNode->_pPre = _pHead->_pPre;
		pNewNode->_pPre->_pNext = pNewNode;
		_pHead->_pPre = pNewNode;
	}

	void PopBack()
	{
		PNode pDelNode = _pHead->_pPre;
		if (pDelNode)
		{
			pDelNode->_pPre->_pNext = _pHead;
			_pHead->_pPre = pDelNode->_pPre;

			delete pDelNode;
		}
	}

	void PushFront(const T& data)
	{
		PNode pNewNode = new Node(data);
		pNewNode->_pPre = _pHead;
		pNewNode->_pNext = _pHead->_pNext;
		_pHead->_pNext = pNewNode;
		pNewNode->_pNext->_pPre = pNewNode;
	}

	void PopFront()
	{
		PNode pDelNode = _pHead->_pNext;
		if (pDelNode)
		{
			pDelNode->_pNext->_pPre = _pHead;
			_pHead->_pNext = pDelNode->_pNext;

			delete pDelNode;
		}
	}

	PNode Insert(PNode pos, const T& data)
	{
		assert(pos);
		PNode pNewNode = new Node(data);
		pNewNode->_pNext = pos;
		pNewNode->_pPre = pos->_pPre;
		pos->_pPre = pNewNode;
		pNewNode->_pPre->_pNext = pNewNode;

		return pNewNode;
	}

	PNode Erase(PNode pos)
	{
		assert(pos);
		PNode pRet = pos->_pNext;

		pos->_pPre->_pNext = pos->_pNext;
		pos->_pNext->_pPre = pos->_pPre;

		delete pos;

		return pRet;
	}

	void Clear()
	{
		PNode pDelNode = _pHead->_pNext;
		// 头删
		while (pDelNode != _pHead)
		{
			_pHead->_pNext = pDelNode->_pNext;
			delete pDelNode;
			pDelNode = _pHead->_pNext;
		}

		_pHead->_pNext = _pHead;
		_pHead->_pPre = _pHead;
	}
private:
	void _CreateHead()
	{
		_pHead = new Node;
		_pHead->_pNext = _pHead;
		_pHead->_pPre = _pHead;
	}
private:
	PNode _pHead;
};

// Iterator
#include <iostream>
using namespace std;

void TestList()
{
	List<int> l;
	l.PushBack(1);
	l.PushBack(2);
	l.PushBack(3);
	l.PushBack(4);
	l.PushBack(5);

	List<int>::Iterator it = l.Begin();
	while (it != l.End())
	{
		cout << *it << " ";
		//cout << it->_data << " ";
		++it;
	}
}

猜你喜欢

转载自blog.csdn.net/wm12345645/article/details/82809984
今日推荐