数据结构::模拟STL实现list
来源:互联网 发布:java虚拟机的内存布局 编辑:程序博客网 时间:2024/06/10 04:20
注意:在STL里面运用的是空间配置器,但是这次我暂时没有用空间配置器,后面我会带上空间配置器再实现一遍。
#include<iostream>#include<assert.h>using namespace std;template<typename T>struct _ListNode{T _data;_ListNode<T>* _pNext;_ListNode<T>* _pPre;_ListNode(const T& x):_data(x),_pNext(NULL),_pPre(NULL){}};template<typename T, typename Ref,typename Ptr>struct _ListIterator{typedef _ListNode<T> Node;typedef _ListIterator<T,T&,T*> Iterator;typedef _ListIterator<T,Ref,Ptr> Self;Node* _node;_ListIterator(){}_ListIterator(Node* node):_node(node){}bool operator != (const Self& s){return _node != s._node;}bool operator == (const Self& s){return _node == s._node;}//前置加加Self& operator++(){_node = _node->_pNext;return *this;}//后置加加Self operator++(int){Self temp(*this);_node = _node->_pNext;return temp;}Ref operator*(){return _node->_data; //(*_node)._data;}Ptr operator->(){return &_node->_data;}//前置减减Self& operator--(){_node = _node->_pPre;return *this;}Self operator--(int){Self temp(*this);_node = _node->_pPre;return temp;}};template<typename T>class _List{typedef _ListNode<T> Node;public:typedef _ListIterator<T,T&,T*> Iterator;typedef _ListIterator<T,const T&,const T*> ConstIterator;public:Node* BuyNode(const T& x){return new Node(x);}_List():_pHead(BuyNode(T())) //参数可以传一个临时对象{_pHead->_pNext = _pHead;_pHead->_pPre = _pHead;}//插入数据void Push(const T&x){/*Node* pCur = BuyNode(x);Node* pTail = _pHead->_pNext;pTail->_pNext = pCur;pCur->_pPre = pTail;_pHead->_pNext = pCur;pCur->_pPre = _pHead;*/Insert(End(),x);}//删除数据void Pop(){Erase(--End());}//头插void PushFront(const T& x){Insert(Begin(),x);}//头删void PopFront(){Erase(Begin());}//在特定位置插入一段区间template <typename InputIterator>void Insert(Iterator pos,InputIterator first, InputIterator last){while(first != last){Insert(pos,*first);++first;}}//在特定的位置插入数据Iterator Insert(Iterator pos, const T& x){assert(pos._node);Node* pCur = pos._node;Node* pPre = pos._node->_pPre;Node* temp = new Node(x);pPre->_pNext = temp;temp->_pPre = pPre;pCur->_pPre = temp;temp->_pNext = pCur;return temp;}//删除特定位置的数据Iterator Erase(Iterator pos){assert(pos._node && pos._node != _head); //判断此位置是否合法并且此位置不等于头节点Node* pCur = pos._node->_pNext;Node* pPre = pos._node->_pPre;pCur->_pPre = pPre;pPre->_pnext = pCur;delete pos._node;return Iterator(pCur);}//const Iterator Begin()const{return _pHead->_pNext;}const Iterator End()const{return _pHead;}Iterator Begin(){return _pHead->_pNext;}Iterator End(){return _pHead;}protected:Node* _pHead;};/***********************测试函数************************///测试插入数据和删除数据void Test1(){_List<int> l1;l1.Push(1);l1.Push(2);l1.Push(3);l1.Push(4);_List<int>::Iterator it1 = l1.Begin();while(it1 != l1.End()){cout<<*it1<<" ";++it1;}cout<<endl;}//头插头删void Test2(){_List<int> l1;l1.Push(1);l1.Push(2);l1.Push(3);l1.Push(4);l1.PushFront(5);_List<int>::Iterator it1 = l1.Begin();while(it1 != l1.End()){cout<<*it1<<" ";++it1;}cout<<endl;}//在特定的位置插入数据void Test3(){_List<int> l1;l1.Push(1);l1.Push(2);l1.Push(3);l1.Push(4);_List<int>::Iterator it1 = l1.Begin();it1 = l1.Insert(it1,6); //Insert函数有返回值,所以用迭代器返回while(it1 != l1.End()){cout<<*it1<<" ";++it1;}cout<<endl;}//测试插入一段区间void Test4(){_List<int> l1;l1.Push(1);l1.Push(2);l1.Push(3);l1.Push(4);_List<int> l2;l2.Push(12);l2.Push(22);l2.Push(32);l2.Push(42);_List<int>::Iterator it1 = l1.Begin();l1.Insert(it1,l2.Begin(),l2.End());it1 = l1.Begin();while(it1!= l1.End()){cout<<*it1<<" ";++it1;}cout<<endl;}int main(){Test1();Test2();Test3();Test4();return 0;}
0 0
- 数据结构::模拟STL实现list
- <STL>模拟实现List
- STL-模拟实现List
- 【STL】模拟实现list
- 模拟实现STL中的list
- 模拟实现STL中的list
- STL模拟实现List+迭代器
- 【STL】list的应用和模拟实现
- 【STL】 list的应用及模拟实现
- 【STL】list的应用模拟实现
- 【STL】迭代器与List的模拟实现
- 【STL】list迭代器的模拟实现
- 模拟实现STL中的List容器
- 类模板模拟实现STL中List
- 使用List模拟实现STL中的Queue
- 迭代器模拟实现STL中的list
- 【C++】模拟实现STL中的list
- 【数据结构】向STL看齐-模拟实现红黑树
- 伪代码的写法
- 为什么j2ee的web研发过程中,有些jsp、html页面中,没有引入某个js(如test.js),却使用了test.js的某个函数,并且系统能够正常运行??
- zeromq中zmq_connect
- linux命令_从菜鸟到菜鸟
- php curl post请求,java后台用request文件流方式接收
- 数据结构::模拟STL实现list
- marathon如何查看一个container运行了多久
- MYSQL分库分表总结
- 原生js的一些研究和总结(1)
- 解决XML警告"No grammar constraints (DTD or XML Schema) referenced in the document"
- intellij idea方法模板
- [ Laravel 5.2 文档 ] 服务 —— 缓存
- Codeforces 458A/51nod 1491 黄金系统【思维】好题~
- zeromq 中的 zmq_ctx_destory