欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ 模擬實(shí)現(xiàn)list(迭代器)實(shí)現(xiàn)代碼

 更新時(shí)間:2017年05月22日 15:08:57   投稿:lqh  
這篇文章主要介紹了C++ 模擬實(shí)現(xiàn)list(迭代器)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下

C++ 模擬實(shí)現(xiàn)list(迭代器)

實(shí)現(xiàn)代碼:

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

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 項(xiàng)目之C++如何實(shí)現(xiàn)數(shù)據(jù)庫連接池

    項(xiàng)目之C++如何實(shí)現(xiàn)數(shù)據(jù)庫連接池

    這篇文章主要介紹了項(xiàng)目之C++如何實(shí)現(xiàn)數(shù)據(jù)庫連接池問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • C++中指針和引用的區(qū)別詳解

    C++中指針和引用的區(qū)別詳解

    這篇文章主要介紹了C++中指針和引用的區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • QT實(shí)戰(zhàn)之打開最近文檔功能的實(shí)現(xiàn)

    QT實(shí)戰(zhàn)之打開最近文檔功能的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)打開最近文檔功能,并實(shí)現(xiàn)基本的新建、打開、保存、退出、幫助等功能,感興趣的可以動(dòng)手嘗試一下
    2022-06-06
  • C/C++ 實(shí)現(xiàn)簡(jiǎn)易HTTP服務(wù)器的示例

    C/C++ 實(shí)現(xiàn)簡(jiǎn)易HTTP服務(wù)器的示例

    這篇文章主要介紹了C/C++ 實(shí)現(xiàn)簡(jiǎn)易HTTP服務(wù)器的示例,幫助大家更好的理解和學(xué)習(xí)C/C++編程,感興趣的朋友可以了解下
    2020-10-10
  • C語言實(shí)現(xiàn)房屋管理系統(tǒng)

    C語言實(shí)現(xiàn)房屋管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)房屋管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C++在成員函數(shù)中使用STL的find_if函數(shù)實(shí)例

    C++在成員函數(shù)中使用STL的find_if函數(shù)實(shí)例

    這篇文章主要介紹了C++在成員函數(shù)中使用STL的find_if函數(shù)實(shí)例,包括了STL中find_if函數(shù)的具體用法及相關(guān)的完整實(shí)例,非常具有參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C語言 變量詳解及示例代碼

    C語言 變量詳解及示例代碼

    本文主要介紹C語言 變量,這里整理了C語言 變量的基礎(chǔ)知識(shí),并附有示例代碼和詳細(xì)講解,希望能幫助學(xué)習(xí)C語言的朋友
    2016-08-08
  • C++紅黑樹的底層實(shí)現(xiàn)機(jī)制詳解

    C++紅黑樹的底層實(shí)現(xiàn)機(jī)制詳解

    紅黑樹與AVL樹一樣,也是一種自平衡的二叉搜索樹,它在每個(gè)結(jié)點(diǎn)上增加一個(gè)存儲(chǔ)位表示結(jié)點(diǎn)的顏色,可以是Red或Black,通過對(duì)任何一條從根到葉子的路徑上各個(gè)結(jié)點(diǎn)著色方式的限制,本文介紹了C++紅黑樹的底層實(shí)現(xiàn)機(jī)制,需要的朋友可以參考下
    2024-08-08
  • 全面解析C++中的析構(gòu)函數(shù)

    全面解析C++中的析構(gòu)函數(shù)

    這篇文章主要介紹了C++中的析構(gòu)函數(shù),為C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01
  • 深入VC回調(diào)函數(shù)的使用詳解

    深入VC回調(diào)函數(shù)的使用詳解

    本篇文章是對(duì)VC回調(diào)函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論