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

 更新时间:2017年05月22日 15:08:57   投稿:lqh  
这篇文章主要介绍了C++ 模拟实现list(迭代器)实现代码的相关资料,需要的朋友可以参考下

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

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

相关文章

  • 八皇后问题实现代码分享

    八皇后问题实现代码分享

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例,这篇文章主要介绍了八皇后问题实现代码,需要的朋友可以参考下
    2014-02-02
  • C++设置事件通知线程工作的方法

    C++设置事件通知线程工作的方法

    这篇文章主要介绍了C++设置事件通知线程工作的方法,是Windows应用程序设计中非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • C++知识点之inline函数、回调函数和普通函数

    C++知识点之inline函数、回调函数和普通函数

    这篇文章主要给大家介绍了关于C++知识点之inline函数、回调函数和普通函数的相关使用方法,以及回调函数和普通函数的区别小结,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-07-07
  • Qt实现手动切换多种布局的完美方案

    Qt实现手动切换多种布局的完美方案

    通过点击程序界面上不同的布局按钮,使主工作区呈现出不同的页面布局,多个布局之间可以通过点击不同布局按钮切换,支持的最多的窗口为9个,不同布局下窗口数随之变化,这篇文章主要介绍了Qt实现手动切换多种布局的完美方案,需要的朋友可以参考下
    2024-07-07
  • C++替换栈中和.data中的cookie实现步骤详解

    C++替换栈中和.data中的cookie实现步骤详解

    这篇文章主要介绍了C++替换栈中和.data中的cookie实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • C语言动态数组详解

    C语言动态数组详解

    本文给大家分享的是一则使用C语言实现动态数组的代码,完美解决内存溢出以及内存回收问题,有需要的小伙伴可以参考下
    2021-09-09
  • 详解C语言进程同步机制

    详解C语言进程同步机制

    这篇文章主要介绍了详解C语言进程同步机制的的相关资料,文中代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • 分享一下8年C++面向对象设计的经验体会

    分享一下8年C++面向对象设计的经验体会

    关于C++程序设计的书藉非常多,本章不讲C++的语法,只讲一些小小的编程道理。如果我能早几年明白这些小道理,就可以大大改善数十万行程序的质量了
    2017-07-07
  • C字符串与C++中string的区别详解

    C字符串与C++中string的区别详解

    以下是对C字符串与C++中string的区别进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-09-09
  • Qt实现绘制一个简单多边形的示例代码

    Qt实现绘制一个简单多边形的示例代码

    QT提供了图形绘制接口QPainter,通过该接口可以绘制多种图形,包括多边形。本文就来利用它实现绘制一个简单的多边形,感兴趣的可以尝试一下
    2022-11-11

最新评论