C++ 模拟实现list(迭代器)实现代码

所属分类: 软件编程 / C 语言 阅读数: 37
收藏 0 赞 0 分享

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; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

详解C++ string字符串类

这篇文章主要介绍了C++ string字符串类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C++单例类模板详解

这篇文章主要介绍了C++单例类模板,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C语言实现数据结构迷宫实验

这篇文章主要为大家详细介绍了C语言实现数据结构迷宫实验,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言数据结构之迷宫问题

这篇文章主要为大家详细介绍了C语言数据结构之迷宫问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言数据结构之迷宫求解问题

这篇文章主要为大家详细介绍了C语言数据结构之迷宫求解问题,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言实现小学生考试系统

这篇文章主要为大家详细介绍了C语言实现小学生考试系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言实现小学生随机出题测试计分

这篇文章主要为大家详细介绍了C语言实现小学生随机出题测试计分,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言实现小学生计算机辅助教学系统

这篇文章主要为大家详细介绍了C语言实现小学生计算机辅助教学系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解C++中构造函数,拷贝构造函数和赋值函数的区别和实现

这篇文章主要介绍了C++中构造函数,拷贝构造函数和赋值函数的区别和实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C语言清除scanf()缓存的案例讲解

今天小编就为大家分享一篇关于C语言清除scanf()缓存的案例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多