#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
template<class T>
struct ListNode
{
ListNode(const T& data = T())
: _pNext(NULL)
, _pPre(NULL)
, _data(data)
{}
ListNode* _pNext;
ListNode* _pPre;
T _data;
};
template<class T>
class ListIterator
{
typedef ListNode* PNode;
typedef ListIterator Self;
public:
ListIterator()
: _pCur(NULL)
{}
ListIterator(PNode pCur)
: _pCur(pCur)
{}
ListIterator(const Self& s)
: _pCur(s._pCur)
{}
Self& operator++()
{
_pCur = _pCur->_pNext;
return *this;
}
Self operator++(int)
{
Self temp(*this);
_pCur = _pCur->_pNext;
return temp;
}
Self& operator--()
{
_pCur = _pCur->_pPre;
return *this;
}
Self operator--(int)
{
Self temp(*this);
_pCur = _pCur->_pPre;
return *this;
}
bool operator!=(const Self& s)
{
return _pCur != s._pCur;
}
bool operator==(const Self& s)
{
return _pCur == s._pCur;
}
T& operator*()
{
return _pCur->_data;
}
T* operator->()
{
return &(_pCur->_data);
}
private:
PNode _pCur;
};
template<class T>
class List
{
typedef ListNode Node;
typedef Node* PNode;
public:
typedef ListIterator Iterator;
public:
List()
{
CreateListHead();
}
List(T* array, size_t size)
{
CreateListHead();//先有一个表头结点
for (int i = 0; i < size; i++)//不用memcpy函数:会产生浅拷贝
{
PushBack(array[i]);//尾插元素
}
}
List(const List& l)
{
_phead = new Node;
PNode ptail = l->pNext;
while (ptail == l)
{
PushBack(ptail->data);
ptail = ptail->pNext;
}
}
List& operator=(const List& l)
{
if (this == &l)
{
return *this;
}
_phead = new Node;
PNode ptail = l->pNext;
while (ptail == l)
{
PushBack(ptail->data);
ptail = ptail->pNext;
}
}
~List()
{
Clear();//清空链表中的元素,表头结点还在
delete _pHead;
_pHead = NULL;
}
Iterator Begin()
{
return Iterator(_pHead->_pNext);
}
Iterator End()
{
return Iterator(_pHead);
}
void PushBack(const T& data)
{
PNode ptail = _pHead->_pPre;
PNode pNewNode = new Node(data);
ptail->_pNext = pNewNode;
pNewNode->_pNext = _pHead;
pNewNode->_pPre = ptail;
_pHead->_pPre = pNewNode;
}
void PopBack()
{
PNode ptail = _pHead->_pPre;
ptail->_pPre->_pNext = _pHead;
_pHead->_pPre = ptail->_pPre;
delete ptail;
}
void PushFront(const T& data)
{
PNode ptail = new Node(data);
ptail->_pNext = _pHead->_pNext;
-pHead->_pNext->_pPre = ptail;
ptail->_pPre = _pHead;
_pHead->_pNext = ptail;
}
void PopFront()
{
if (_pHead->_pNext == _phead)
return;
else
{
PNode ptail = _pHead->_pNext;
_pHead->_pNext = ptail->_pNext;
ptail->_pNext->pPre = _pHead;
delete ptail;
}
}
PNode Insert(PNode pos, const T& data)
{
PNode ptail = pos->_pPre;
PNode pNewNode = new Node(data);
pNewNode->_pNext = ptail;
ptail->_pPre = pNewNode;
pos->_pNext = pNewNode;
pNewNode->_pPre = pos;
}
PNode Erase(PNode pos)
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNest->pPre = pos->_pPre;
delete pos;
}
size_t Size()
{
int count = 0;
PNode ptail = _pHead->_pNext;
while (ptail != _pHead)
{
count++;
ptail = ptail->_pNext;
}
return count;
}
// 将链表中的结点清空
void Clear()
{
PNode ptail = _pHead->_pNext;
while (ptail != _pHead)
{
_pHead->_pNext = ptail->_pNext;
delete ptail;
ptail = _pHead->_pNext;
}
ptail->_pPre = _pHead;
}
bool Empty()const
{
return _pHead->_pNext - _pHead;
}
private:
void CreateListHead()
{
_pHead = new Node;
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
}
private:
PNode _pHead;
};
template <class Iterator>
Iterator Find(Iterator start, Iterator finish, const T& data)
{
while (start != finish)
{
if (*start == data)
return start;
++start;
}
return start;
}
int main(void)
{
size_t i;
List<int> L1;
L1.PushBack(1);
L1.PushBack(2);
L1.PushBack(3);
L1.PushBack(4);
L1.PopBack();
List<int>::Iterator it = L1.Begin();
while (L1.End() != it)
{
cout << *it << " ";
++it;
}
cout << endl;
i = L1.Size();
cout << L1.Empty() << endl;
cout << i << endl;
cout << L1.Empty() << endl;
i = L1.Size();
cout << i << endl;
return 0;
}
模拟实现C++中的ListIterator函数
最新推荐文章于 2024-05-03 14:39:15 发布