模拟实现容器List(迭代器)
2017-10-28 23:41
309 查看
#include <iostream> using namespace std; template<class T> struct ListNode { ListNode(const T& data = T()) : _pPre(0) , _pNext(0) , _data(data) {} ListNode* _pPre; ListNode* _pNext; T _data; }; //迭代器 template <class T,class Ref,class Pointer> class ListIterator { typedef ListIterator Self; public: ListIterator() : _pCur(0) {} ListIterator(ListNode<T>* pCur) : _pCur(pCur) {} ListIterator(const Self& s) : _pCur(s._pCur) {} Ref operator*() { return _pCur->_data; } Ref operator->() { /*return &(operator*());*/ return &(_pCur->_data); } 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 temp; } bool operator!=(const Self& s) { return _pCur != s._pCur; } bool operator==(const Self& s) { return _pCur == s._pCur; } ListNode<T>* _pCur; }; template <class T> class List { public: typedef ListIterator<T,T&,T*> Iterator; typedef ListNode<T>* Node; public: List() : _pHead(new ListNode<T>) { _pHead->_pNext = _pHead;//头结点 _pHead->_pPre = _pHead; } List(const T* array, size_t size) :_pHead(new ListNode<T>) { _pHead->_pNext = _pHead;//头结点 _pHead->_pPre = _pHead; for (size_t i = 0; i < size; ++i) { PushBack(array[i]); } } List(const List& l)//拷贝构造 :_pHead(new ListNode<T>) { _pHead->_pNext = _pHead;//头结点 _pHead->_pPre = _pHead; Node first = l._pHead->_pNext; while (first != l._pHead->_pPre) { PushBack(first->_data); first = first->_pNext; } } List& operator=(const List& l) { if (this != &l) { _pHead = l._pHead; Iterator it = Begin(); while (it != End()) { Insert(it); it++; } } } ~List() { Clear(); delete _pHead; _pHead = NULL; } /////////////////////////////////////////////////////// Iterator Begin() { return Iterator(_pHead->_pNext); } Iterator End() { return Iterator(_pHead); } /////////////////////Modify////////////////////////// void PushBack(const T& data) { Node pTail = _pHead->_pPre;//尾指针 Node pNewNode = new ListNode<T>(data); if (Empty())//空链表 { _pHead->_pNext = pNewNode; pNewNode->_pNext = _pHead; _pHead->_pPre = pNewNode; pNewNode->_pPre = _pHead; } else//链表非空 { Node pTail = _pHead->_pPre;//尾指针 pTail->_pNext = pNewNode; pNewNode->_pPre = pTail; _pHead->_pPre = pNewNode; pNewNode->_pNext = _pHead; } } void PopBack() { if (Empty()) return; else { Node pTail = _pHead->_pPre;//尾指针 pTail = _pHead->_pPre->_pPre; delete pTail->_pNext; pTail->_pNext = _pHead; _pHead->_pPre = pTail; } } void PushFront(const T& data) { Node pTail = _pHead->_pPre;//尾指针 Node pNewNode = new ListNode<T>(data); pNewNode->_pNext = _pHead->_pNext; pNewNode->_pPre = _pHead; _pHead->_pNext->_pPre = pNewNode; _pHead->_pNext = pNewNode; } void PopFront() { if (Empty()) return; else { Node tmp = _pHead->_pNext; _pHead->_pNext = tmp->_pNext; tmp->_pNext->_pPre = _pHead; delete tmp; } } Iterator Insert(Iterator pos, const T& data) { Node pNewNode = new ListNode<T>(data); pNewNode->_pNext = pos._pCur; pNewNode->_pPre = pos._pCur->_pPre; pos._pCur->_pPre->_pNext = pNewNode; pos._pCur->_pPre = pNewNode; return Iterator(pNewNode); } Iterator Erase(Iterator pos) { Node next = pos._pCur->_pNext; pos._pCur->_pPre->_pNext = pos._pCur->_pNext; pos._pCur->_pNext->_pPre = pos._pCur->_pPre; delete pos._pCur; return Iterator(next); } bool Empty()const { if (_pHead->_pNext == _pHead) return true; else return false; } size_t Size()const { int cout = 0; Node pnext = _pHead; Node pTail = _pHead->_pPre; while (pnext != pTail) { cout++; pnext = pnext->_pNext; } } 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; } void Clear() { Iterator it = Begin(); while (it != End()) { it = Erase(it); } _pHead->_pNext = _pHead; _pHead->_pPre = _pHead; } private: ListNode<T>* _pHead; };
测试函数
#include "List.h"
void test()
{
int array[] = { 0, 1, 2, 3, 4 };
List<int> l(array, sizeof(array) / sizeof(array[0]));
l.PushBack(0);
l.PushBack(1);
l.PushBack(2);
List<int>::Iterator it = l.Begin();
while (it != l.End())
{
cout << *it << "->";
++it;
}
cout << endl;
l.PopFront();
it = l.Begin();
while (it != l.End())
{
cout << *it << "->";
++it;
}
cout << endl;
l.PopBack();
it = l.Begin();
while (it != l.End())
{
cout << *it << "->";
++it;
}
cout << endl;
l.PopFront();
it = l.Begin();
while (it != l.End())
{
cout << *it << "->";
++it;
}
cout << endl;
}
void test2()
{
List<int> l;
l.PushBack(1);
l.PushBack(1);
l.PushBack(1);
l.PushBack(1);
l.PushBack(1);
List<int> L1(l);
List<int>::Iterator it = L1.Begin();
while (it != L1.End())
{
cout << *it << "->";
++it;
}
cout << endl;
List<int> L2 = L1;
it = L1.Begin();
while (it != L1.End())
{
cout << *it << "->";
++it;
}
cout << endl;
}
int main()
{
test2();
system("pause:");
return 0;
}
相关文章推荐
- STL容器中list与迭代器iterator的模拟实现
- C++模拟实现容器list(含迭代器)
- 模拟实现STL下的list容器
- 模拟实现STL中list(用迭代器)
- 迭代器模拟实现STL中的list
- 以模板模拟实现List容器
- 模拟实现list(迭代器)
- 模拟实现STL中的List容器
- STL--> list 双向循环链表容器 接口使用及介绍。 模拟实现 STL list容器
- 【STL】迭代器与List的模拟实现
- C++ 模拟实现list(迭代器)实现代码
- JAVA容器-模拟LinkedList实现(双链表)
- 使用模板类实现List容器&迭代器
- 模拟实现容器vector(含迭代器、类型萃取)
- 浅谈STL list<T>链表容器和迭代器的使用C++实现
- 模拟实现list(迭代器)
- 模拟实现容器适配器 stack queue
- Java中容器[Collection(List,Set,Queue),Map],迭代器(Iterator)和比较器(Comparator)及列表排序
- 用迭代器实现list的各种功能
- SPOJ - ADALIST - Ada and List - 模拟+STL容器 - Mutual Training for Wannafly Union #7