模拟实现STL中的List容器
2017-03-23 20:48
375 查看
List.h
#include <iostream> #include <assert.h> using namespace std; //结点结构 template<class T> struct ListNode { ListNode(const T&data = T()) :_prev(0) ,_next(0) ,_data(data) {} ListNode<T>*_prev; ListNode<T>*_next; T _data; }; //构造迭代器 template<class T,class Ref,class Ptr> class _Listlterator_ { typedef _Listlterator_<T,T&,T*> lterator; typedef _Listlterator_<T,const T&,const T*> Constlterator; typedef ListNode<T>* _LinkType; typedef _Listlterator_<T,Ref,Ptr> Self; typedef Ref Reference; typedef Ptr Pointer; public: _Listlterator_(_LinkType x = 0) :_node(x) {} _Listlterator_(const lterator& x) :_node(x->_node) {} bool operator==(const lterator&x) { return _node==(x._node); } bool operator!=(const lterator&x) { return _node!=(x._node); } Reference operator*() { return _node->_data; } Pointer operator->() { return &_node->_data; } Self& operator++() { _node = _node->_next; return *this; } Self operator++(int) { Self tem(*this); _node = _node->_next; return tem; } Self&operator--() { _node = _node->_prev; return *this; } Self operator--(int) { Self tem(*this); _node = _node->_prev; return tem; } public: _LinkType _node; }; //实现List的功能 template<class T> class List { public: typedef ListNode<T> Node; typedef T ValueType; typedef ValueType& Reference; typedef const ValueType& ConstReference; typedef ValueType* Pointer; typedef const ValueType* ConstPointer; typedef Node* LinkType; typedef size_t SizeType; typedef _Listlterator_<T,T&,T*> lterator; typedef _Listlterator_<T,const T&,const T*>Constlterator; public: List() { _phead = new Node ; _phead->_data = T(); _phead->_next = _phead; _phead->_prev = _phead; } List(SizeType n,const T&data) :_phead( new Node) { _phead ->_data = T(); _phead->_next =_phead ; _phead->_prev = _phead; LinkType _temp = _phead; while (n) { LinkType tem = new Node; tem->_data = data; if (tem!=NULL) { _temp ->_next= tem; tem ->_prev = _temp; tem ->_next = _phead; _phead->_prev = tem; _temp = tem; --n; } } } List(const List<T>&l)//拷贝构造函数 :要注意浅拷贝的问题 { _phead = new Node(); _phead->_data = T(); _phead->_prev = _phead; _phead->_next = _phead; LinkType tem = l._phead->_next; LinkType pre = _phead; while (tem != l._phead) { LinkType temp = new Node ; temp->_data = tem->_data; pre->_next = temp; temp ->_prev = pre; temp ->_next = _phead; _phead->_prev = temp; pre = temp; tem = tem->_next; } } ~List() { Destroy(); delete _phead; _phead = NULL; } void Destroy() { LinkType ptem = _phead->_next; while (ptem!=_phead) { LinkType tem = ptem->_next; delete ptem; ptem = tem; } _phead->_next = _phead; _phead->_prev = _phead; } List<T>& operator=(constList<T>&l)//重载赋值运算符 { _phead = new Node(); _phead->_data = T(); _phead->_prev = _phead; _phead->_next = _phead; LinkType tem = l._phead->_next; LinkType pre = _phead; while (tem) { LinkType temp = new Node ; temp ->_data = tem->_data; pre->_next = temp; temp ->_prev = pre; temp ->_next = _phead; _phead->_prev = temp; pre = temp; tem = tem->_next; } return *this; } ////////////////////////////////////////////////////////////// lterator Begin() { return _phead->_next; } Constlterator Begin()const { return _phead->_next; } lterator End() { return _phead; } bool Empty()const { return _phead->_next==_phead; } SizeType Size()const { SizeType n = 0; LinkType temp = _phead; while (temp->_next!=_phead) { ++n; temp = temp->_next; } return n; } SizeType MaxSize()const {} Reference Front() { return _phead->_next->_data; } Reference Back() { return _phead->_prev->_data; } ConstReference Back()const { return *_phead->_prev; } lterator Insert(lterator pos,const T& x=T()) { if(pos==NULL) { return NULL; } LinkType temp = new Node ; temp->_data = x; LinkType prve = pos._node->_prev; prve->_next = temp; temp->_prev = prve; temp->_next = pos._node; pos._node->_prev = temp; return _phead; } void PushFront(const T&x) { Insert(Begin(),x); } void PushBack(const T&x) { Insert(End(),x); } lterator Erase(lterator pos) { if (pos==NULL) { return NULL; } if (pos == _phead) { pos = _phead->_prev; } LinkType prve = pos._node->_prev; prve->_next = pos._node->_next; prve->_next->_prev = prve; delete pos._node;//要释放不用的指针 return _phead; } void PopFront() { Erase(Begin()); } void PopBack() { Erase(End()); } void Resize(SizeType sz,T c = T())//置数函数 { if (sz<=Size()) { return; } else { SizeType n = sz -Size(); for (SizeType i = 0;i<n;i++) { PushBack(c); } } } void Assign(SizeType n,const T&data) { Destroy(); while (n--) { PushBack(data); } } void Clear() { Destroy(); } private: void EmptyInit() {} protected: LinkType _phead; }; void PrintfList(List<int> l)//利用迭代器打印list的内容 { List<int>::lterator it1 = l.Begin(); while (it1 != l.End()) { cout<<it1._node->_data<<" "; ++it1; } cout<<endl; }
test.c
/////////////////////////////////////////////////////////////////////// #include "ListAqueue.h" int main() { List<int>l1; List<int>l2(3,5); cout<<"l2:"; PrintfList(l2); List<int>l3(l2); cout<<"l3:"; PrintfList(l3); List<int>l4 = l2; cout<<"l4:"; PrintfList(l4); l2.Insert(l2.Begin(),4); cout<<"l2:"; PrintfList(l2); l3.Insert(l3.End(),2); cout<<"l3:"; PrintfList(l3); l2.Erase(l2.Begin()); cout<<"l2:"; PrintfList(l2); l3.Erase(l3.End()); cout<<"l3:"; PrintfList(l3); l1.PushBack(2); l1.PushFront(1); l1.PopBack(); l1.Resize(5,6); cout<<"l1:"; PrintfList(l1); l1.Resize(1,3); cout<<"l1:"; PrintfList(l1); l2.Assign(4,3); cout<<"l2:"; PrintfList(l2); l1.Clear(); cout<<"l1:"; PrintfList(l1); return 0; }
运行结果
相关文章推荐
- STL容器中list与迭代器iterator的模拟实现
- 模拟实现STL下的list容器
- STL--> list 双向循环链表容器 接口使用及介绍。 模拟实现 STL list容器
- 动手实现自己的 STL 容器《2》---- list
- stl容器区别: vector list deque set map-底层实现
- stl容器list部分实现
- JAVA容器-模拟LinkedList实现(双链表)
- 动手实现自己的 STL 容器《2》---- list
- stl容器区别: vector list deque set map及底层实现
- 【STL】 list的应用及模拟实现
- 模拟实现STL中的list
- STL-模拟实现list
- STL 容器区别:vector、list、deque、set、map的底层实现
- 【STL】list的应用模拟实现
- stl容器区别: vector list deque set map-底层实现
- stl容器区别: vector list deque set map及底层实现
- 使用STL中的list容器实现单链表的操作
- 用C++实现STL容器list
- STL-模拟实现List
- 数据结构::模拟STL实现list