最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

實現(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; 
} 

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

相關(guān)文章

  • 八皇后問題實現(xiàn)代碼分享

    八皇后問題實現(xiàn)代碼分享

    八皇后問題,是一個古老而著名的問題,是回溯算法的典型案例,這篇文章主要介紹了八皇后問題實現(xiàn)代碼,需要的朋友可以參考下
    2014-02-02
  • C++設(shè)置事件通知線程工作的方法

    C++設(shè)置事件通知線程工作的方法

    這篇文章主要介紹了C++設(shè)置事件通知線程工作的方法,是Windows應(yīng)用程序設(shè)計中非常實用的技巧,需要的朋友可以參考下
    2014-10-10
  • C++知識點之inline函數(shù)、回調(diào)函數(shù)和普通函數(shù)

    C++知識點之inline函數(shù)、回調(diào)函數(shù)和普通函數(shù)

    這篇文章主要給大家介紹了關(guān)于C++知識點之inline函數(shù)、回調(diào)函數(shù)和普通函數(shù)的相關(guān)使用方法,以及回調(diào)函數(shù)和普通函數(shù)的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • Qt實現(xiàn)手動切換多種布局的完美方案

    Qt實現(xiàn)手動切換多種布局的完美方案

    通過點擊程序界面上不同的布局按鈕,使主工作區(qū)呈現(xiàn)出不同的頁面布局,多個布局之間可以通過點擊不同布局按鈕切換,支持的最多的窗口為9個,不同布局下窗口數(shù)隨之變化,這篇文章主要介紹了Qt實現(xiàn)手動切換多種布局的完美方案,需要的朋友可以參考下
    2024-07-07
  • C++替換棧中和.data中的cookie實現(xiàn)步驟詳解

    C++替換棧中和.data中的cookie實現(xiàn)步驟詳解

    這篇文章主要介紹了C++替換棧中和.data中的cookie實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • C語言動態(tài)數(shù)組詳解

    C語言動態(tài)數(shù)組詳解

    本文給大家分享的是一則使用C語言實現(xiàn)動態(tài)數(shù)組的代碼,完美解決內(nèi)存溢出以及內(nèi)存回收問題,有需要的小伙伴可以參考下
    2021-09-09
  • 詳解C語言進程同步機制

    詳解C語言進程同步機制

    這篇文章主要介紹了詳解C語言進程同步機制的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 分享一下8年C++面向?qū)ο笤O(shè)計的經(jīng)驗體會

    分享一下8年C++面向?qū)ο笤O(shè)計的經(jīng)驗體會

    關(guān)于C++程序設(shè)計的書藉非常多,本章不講C++的語法,只講一些小小的編程道理。如果我能早幾年明白這些小道理,就可以大大改善數(shù)十萬行程序的質(zhì)量了
    2017-07-07
  • C字符串與C++中string的區(qū)別詳解

    C字符串與C++中string的區(qū)別詳解

    以下是對C字符串與C++中string的區(qū)別進行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
    2013-09-09
  • Qt實現(xiàn)繪制一個簡單多邊形的示例代碼

    Qt實現(xiàn)繪制一個簡單多邊形的示例代碼

    QT提供了圖形繪制接口QPainter,通過該接口可以繪制多種圖形,包括多邊形。本文就來利用它實現(xiàn)繪制一個簡單的多邊形,感興趣的可以嘗試一下
    2022-11-11

最新評論

曲沃县| 上杭县| 镇江市| 德令哈市| 湘阴县| 自治县| 高尔夫| 涡阳县| 长春市| 苏州市| 察隅县| 博野县| 铅山县| 池州市| 台北市| 安西县| 镇远县| 灵川县| 华安县| 固安县| 舟曲县| 和硕县| 镇巴县| 蒙自县| 青神县| 青龙| 屏山县| 姜堰市| 图们市| 平潭县| 肃北| 巴塘县| 元氏县| 九江县| 和林格尔县| 长宁县| 裕民县| 丹阳市| 耒阳市| 乃东县| 永清县|