zoukankan      html  css  js  c++  java
  • 熟悉模板操作--简单的list

      1 #ifndef _LIST_H__
      2 #define _LIST_H__
      3 #include<stdlib.h>
      4 template<class Type> class List;
      5 
      6 
      7 template<class Type>
      8 class Link
      9 {
     10     friend class List<Type>;
     11     
     12 private:
     13     Type m_Data;
     14     Link<Type>* m_Next;
     15     Link<Type>* m_Pre;
     16     Link(const Type& Val, Link<Type>* Next, Link<Type>* Pre);
     17 
     18 };
     19 
     20 template<class Type>
     21 Link<Type>::Link(const Type& Val, Link<Type>* Next, Link<Type>* Pre):m_Data(Val), m_Next(Next),m_Pre(Pre)
     22 {
     23     
     24 }
     25 
     26 template <class Type>
     27 class List
     28 {
     29 public:
     30     class Iterator
     31     {
     32     private:
     33         List<Type>* m_pList;
     34         Link<Type>* m_pLink;
     35     public:
     36         Iterator( List<Type>* pList, Link<Type>* pLink):m_pList(pList), m_pLink(pLink)
     37         {
     38         
     39         }
     40         Iterator():m_pList(NULL), m_pLink(NULL)
     41         {
     42             
     43         }
     44 
     45         Type& operator*()
     46         {
     47             return (m_pLink->m_Data);
     48         }
     49 
     50         Type* operator->()
     51         {
     52             return &(m_pLink->m_Data);
     53         }
     54 
     55         Iterator& operator++()
     56         {
     57             if(m_pLink->m_Next != NULL)
     58             {
     59                 m_pLink = m_pLink->m_Next;
     60             }
     61             else
     62             {
     63                 m_pLink = NULL;
     64             }
     65             return *this;
     66         }
     67 
     68         Iterator operator++(int)
     69         {
     70             Iterator tmp = *this;
     71             ++ *this;
     72             return tmp;
     73         }
     74 
     75         bool operator==(Iterator iter)
     76         {
     77             return (m_pList == iter.m_pList && m_pLink == iter.m_pLink);
     78         }
     79 
     80         bool operator!=(Iterator iter)
     81         {
     82             return (m_pList != iter.m_pList || m_pLink != iter.m_pLink);
     83         }
     84     };
     85 private:
     86     Link<Type>* m_stHead;
     87     int n_element;
     88     Link<Type>* m_stTail;
     89 public:
     90     List():m_stHead(NULL),m_stTail(NULL),n_element(0)
     91     {
     92     
     93     }
     94     ~List()
     95     {
     96     
     97     }
     98 
     99     Iterator begin()
    100     {
    101         return Iterator(this, m_stHead);
    102     }
    103 
    104     Iterator end()
    105     {
    106         return Iterator(this, NULL);
    107     }
    108 
    109     int push_back(const Type& stData)
    110     {
    111         //检查是否是第一个元素
    112         if(NULL == m_stHead)
    113         {
    114             Link<Type>* pLink = new Link<Type>(stData, NULL, NULL);
    115             m_stHead = pLink;
    116             m_stTail = pLink;
    117         }
    118         else
    119         {
    120         //不是最后一个元素
    121             Link<Type>* pLink = new Link<Type>(stData, NULL, m_stTail);
    122             m_stTail->m_Next = pLink;
    123             m_stTail = pLink;
    124         }
    125         n_element ++;
    126         return 0;
    127     }
    128     
    129 };
    130 #endif
    #include "List.h"
    #include <stdio.h>
    int main()
    {
    	List<int> IList;
    	IList.push_back(5);
    	IList.push_back(6);
    	IList.push_back(7);
    
    	List<int>::Iterator iter ;
    	for(iter = IList.begin(); iter != IList.end(); iter ++ )
    	{
    		printf("%d
    ", *iter);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Webpack笔记(三)——一款破产版脚手架的开发
    Google C++命名规范
    视觉词袋模型(BOVW)
    机器学习之四:决策树
    Zernike不变矩
    互联网产品各阶段的标准流程文档
    机器学习之三:logistic回归(最优化)
    Python
    机器学习之二:K-近邻(KNN)算法
    Python学习之二:Python 与 C 区别
  • 原文地址:https://www.cnblogs.com/lzhenf/p/3659900.html
Copyright © 2011-2022 走看看