用迭代器实现list的各种功能
来源:互联网 发布:网易新闻app源码 编辑:程序博客网 时间:2024/05/16 17:53
<span style="font-size:24px;">#include<iostream>#include<cstdlib>#include<list>#include<cassert>using namespace std;template<class T>struct _ListNode{_ListNode* _prev;_ListNode* _next;T _data;_ListNode(const T& x):_data(x),_prev(0),_next(0){}};template<class T,class Ref,class Ptr> //<T,T&,T*>struct _ListIterator{typedef _ListNode<T> Node;typedef _ListIterator<T,Ref,Ptr> Self;_ListIterator(){}_ListIterator(Node* node):_node(node){}Ref operator*(){return _node->_data ;}Ptr operator->() //p->a==(*p).a{return _node; //返回地址}bool operator==(const Self& s){return _node ==s._node ;}bool operator!=(const Self& s){return _node!=s._node ;}Self& operator++() //前置++{_node=_node->_next;return *this;}Self operator++(int){Self tmp(_node);_node=_node->_next ;return tmp;}Self& operator--(){_node=_node->_prev ;return *this;}Self operator--(int){Self tmp(_node);_node=_node->_prev ;return tmp;}Node* _node;};//双向循环列表template<class T>class List{public:typedef _ListNode<T> Node;typedef _ListIterator<T,T&,T*> Iterator;Node* BuyNode(const T& x){Node* node=new Node(x);return node;}List(){_head=BuyNode(T()); //创建一个头节点_head->_next =_head;_head->_prev =_head;}void PushBack(const T& x){ //这个是用普通方法实现的/*Node* tail=_head->_prev ;Node* tmp=BuyNode(x);tail->_next =tmp;tmp->_prev =tail;tmp->_next =_head;_head->_prev =tmp;*/ //这个是用迭代器实现的Insert(End(),x);}void PopBack(){Erase(--End());}void PushFront(const T& x){Insert(Begin(),x);}void PopFront(){Erase(Begin());}void Insert(Iterator pos,const T& x) //在pos之前插入{Node* tmp=BuyNode(x);Node* cur=pos._node ->_prev ;cur->_next =tmp;tmp->_prev =cur;tmp->_next =pos._node ;pos._node ->_prev =tmp;}void Erase(Iterator pos){assert(pos!=End());Node* node=pos._node ;if(pos._node ->_next ==_head){++pos;} ++pos;Node* prev=node->_prev ;Node* next=node->_next ;prev->_next =next;next->_prev =prev;delete node ;}Iterator Begin() //[{return Iterator(_head->_next );}Iterator End() //){return Iterator(_head);}private:Node* _head;};int main(){/*list<int> l;l.push_back (1);l.push_back (2);l.push_back (3);l.push_back (4);list<int>::iterator it=l.begin ();while(it!=l.end()){cout<<*it<<" ";++it;}*///i调用了匿名对象//int i=int(); //i=0//int i=int(2); //i=2//int j=9;//cout<<i<<endl;//cout<<j<<endl;List<int> l;l.PushBack (1);l.PushBack (2);l.PushBack (3);l.PushBack (4);/*l.PushFront (1);l.PushFront (2);l.PushFront (3);l.PushFront (4);*/l.PopFront ();//l.PopBack ();List<int>::Iterator it=l.Begin();while(it!=l.End()){cout<<*it<<" ";++it;}cout<<endl;system("pause");return 0;}</span>
0 0
- 用迭代器实现list的各种功能
- 【ThinkingInJava】49、List的各种功能方法
- 实现各种功能的函数
- 高效实现List反序功能的实现
- String中正则表达式的各种功能,集合Collection,list
- 有向网的各种功能实现
- 基于Spring Boot的各种功能实现
- data list分页功能的实现
- JavaScript实现Java的List功能
- JavaScript实现Java的List功能
- js实现List的部分功能
- 【STL】list的简单剖析以及各种函数的实现
- 【STL】list的简单剖析以及各种函数的实现
- C语言二叉树的各种功能的实现
- 利用数组创建的顺序表实现各种功能
- UITableViewCell的各种功能
- java中List的排序功能的实现
- 【C/C++】List链表的功能实现模型
- Linux1_gcc与g++
- 在职研究生有用吗?
- RN和IOS相互引用
- 深度学习(三十)贪婪深度字典学习
- hive 优化 tips
- 用迭代器实现list的各种功能
- 51nod-1014 X^2 Mod P(二次剩余。。求解决)
- UML类图实例
- jpa中的criteria教程
- Android WiFi模块功能开发
- 用正则表达式去除字符串里的特定的多个字符
- 支付宝支付
- web前端-JavaScript 用法 -001
- RecyclerView的基本使用,特性分析