C++ 模拟实现list(迭代器)实现代码
更新时间:2020年4月25日 17:31 点击:1829
C++ 模拟实现list(迭代器)
实现代码:
#pragma once; #include <assert.h> #include<iostream> #include <assert.h> using namespace std; template<class T> struct __ListNode { T _data; __ListNode<T>* _next; __ListNode<T>* _prev; __ListNode(const T& x) :_data(x) ,_next(NULL) ,_prev(NULL) { } }; template <class T,class Ref,class Ptr > struct __ListIterator { typedef __ListNode<T> Node; typedef __ListIterator<T,Ref,Ptr> Self; __ListIterator(Node* node) :_node(node) { } Ref operator*() { return _node->_data; } Ptr operator->() { return &(_node->_data) } Self& operator++() { _node=_node->_next; return *this; } Self& operator--() { _node=_node->_prev; return *this; } Self operator++(int) { Self tmp=_node; _node=_node->_next; //return tmp; return Self(tmp) } Self operator--(int) { Self tmp=(*this); _node=_node->_prev; return tmp; } bool operator!=(const Self& s) const { return this->_node!=s._node; } bool operator==(const Self& s) const { return this->_node==s._node; } Node* _node; }; template<class T> struct List { typedef __ListNode<T> Node; public: typedef __ListIterator<T,T&,T*> Iterator; typedef __ListIterator<T,const T&,const T*> ConstIterator; Node* GetNode(const T& x) { return new Node(x); } List() { _head=GetNode(T()); _head->_next=_head; _head->_prev=_head; } Iterator Begin() { return Iterator(_head->_next); } Iterator End() { return Iterator(_head); } ConstIterator Begin() const { return ConstIterator(_head->_next); } ConstIterator End() const { return ConstIterator(_head); } void PushBack(const T& x) { /* Node* _tail=_head->_prev; Node* tmp=GetNode(x); _tail->_next=tmp; tmp->_prev=_tail; tmp->_next=_head; _head->_prev=tmp;*/ Insert(End(),x); } void PopBack() { /* assert(_head->_prev ); Node* tail=_head->_prev; Node* prev=tail->_prev; Node* next=tail->_next; prev->_next=next; next->_prev=prev; delete tail;*/ Erase(--End()); } void PushFront(const T& x) { /*assert(_head) Node* tmp=GetNode(x); Node* next=_head->_next; _head->_next=tmp; tmp->_prev=_head; tmp->_next=next; next->_prev=tmp;*/ Insert(Begin(),x); } void PopFront() { /*assert(_head->_next); Node* tmp=_head->_next; Node* next=tmp->_next; _head->_next= next; next->_prev=_head; delete tmp;*/ Erase(Begin()); } Iterator Insert(Iterator pos, const T& x) { assert(pos._node); Node* tmp=GetNode(x); Node* cur=pos._node; Node* prev=cur->_prev; prev->_next=tmp; tmp->_prev=prev; tmp->_next=cur; cur->_prev=tmp; return tmp; } Iterator Erase(Iterator pos) { assert(pos._node && pos._node!=NULL); Node* tmp=pos._node; Node* next=tmp->_next; Node* prev=tmp->_prev; next->_prev=prev; prev->_next=next; delete tmp; return Iterator(next); } protected: Node* _head; }; void PrintList(const List<int>& l) { List<int>::ConstIterator It=l.Begin(); while(It!=l.End()) { cout<<*It<<" "; ++It; } cout<<endl;} void TestList2() { List<int> l2; l2.PushBack(1); l2.PushBack(2); l2.PushBack(3); l2.PushBack(4); l2.PopBack(); l2.PopBack(); l2.PopBack(); l2.PopBack(); l2.PopBack(); PrintList(l2); } void TestList3() { List<int> l3; l3.PushFront(1); l3.PushFront(2); l3.PushFront(3); l3.PushFront(4); l3.PopFront(); l3.PopFront(); l3.PopFront(); PrintList(l3); }
#include "List.h" int main() { //TestList1(); //TestList2(); TestList3(); return 0; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
Java8 实现stream将对象集合list中抽取属性集合转化为map或list
这篇文章主要介绍了Java8 实现stream将对象集合list中抽取属性集合转化为map或list的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-05- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了java8如何用Stream查List对象某属性是否有重复的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-11
- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
pytorch::Dataloader中的迭代器和生成器应用详解
这篇文章主要介绍了pytorch::Dataloader中的迭代器和生成器应用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-30- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 这篇文章主要介绍了Java8处理List的双层循环问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-19
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C# List 排序各种用法与比较的相关资料,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了使用list stream:任意对象List拼接字符串操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-09
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要介绍了C# List介绍及具体用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要介绍了Java List集合返回值去掉中括号('[ ]')的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-29