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

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)文章

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

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

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

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

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

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

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

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

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

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

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

    C語(yǔ)言動(dòng)態(tài)數(shù)組詳解

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

    詳解C語(yǔ)言進(jìn)程同步機(jī)制

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

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

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

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

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

    Qt實(shí)現(xiàn)繪制一個(gè)簡(jiǎn)單多邊形的示例代碼

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

最新評(píng)論

珲春市| 巴南区| 河西区| 桐柏县| 阳江市| 吕梁市| 昂仁县| 浙江省| 灵丘县| 惠州市| 射洪县| 阿巴嘎旗| 清丰县| 花莲县| 保德县| 丰镇市| 陵川县| 沁阳市| 甘洛县| 札达县| 钟山县| 舞钢市| 如东县| 靖州| 龙里县| 登封市| 杭锦后旗| 庄河市| 定日县| 丹寨县| 湘潭县| 扶沟县| 化德县| 年辖:市辖区| 鄂温| 罗田县| 兖州市| 余江县| 常山县| 札达县| 清河县|