list实现

#ifndef LIST_T_H
#define LIST_T_H
//update time :2012-07-30 11:54:12
template<typename T>
class list_t
{
  struct data
  {
    T t;
    data *next;
  };
  data *_start,*_end;
  typedef unsigned int size_t;
  size_t  _count;
public:
  list_t():_start(0),
           _end(0),
           _count(0)
  {
  }
  list_t(const list_t& lst):_start(0),
                            _end(0),
                            _count(0)
  {
     clear();
     copy(lst);
  }
  list_t& operator=(const list_t& lst)
  {
    clear();
    copy(lst);
    return *this;
  }
  ~list_t()
  {
     clear();
  }
  void copy(const list_t& lst)
  {
    for(iterator it=lst.begin();it!=lst.end();++it)
    {
      push_back(*it);
    }
  }
  void clear()
  {
     if(_count>0)
     {
         data *pT=_start;
         while(pT)
         {
           data *pNext=pT->next;
           delete pT;
           pT=pNext;
         }
         _start=0;
         _end=0;
         _count=0;
     }
  }
  bool push_back(const T &t)
  {
      data *pT=new data;
      if(!pT)
      return false;
      pT->t=t;
      pT->next=NULL;
      if(!_start)
      {
          _start=pT;
          _end=pT;
      }
      else
      {
         _end->next=pT;
         _end=pT;
      }
      ++_count;
      return true;
  }
  bool pop_front(T &t)
  {
    if(_count==0)
    return false;
    t=_start->t;
    --_count;
    data* pT=_start->next;
    delete _start;
    _start=pT;
    return true;
  }
  data* erase(const T &t)
  {
      data *pT=_start;
      data *pFront=NULL;
      while(pT)
      {
          if(pT->t==t)
          {
             if(pT==_start)
             {
                 _start=pT->next;
             }
             else
             {
                 if(pT==_end)
                 _end=pFront;

                 pFront->next=pT->next;
             }
             data* pRet=pT->next;
             --_count;
             delete pT;

             return pRet;
          }
          pFront=pT;
          pT=pT->next;
      }
      return NULL;
  }
  data* find(const T &t)
  {
    data *pT=_start;
    while(pT)
    {
       if(pT->t==t)
       break;
       pT=pT->next;
    }
    return pT;
  }
  data* begin()const
  {
    return _start;
  }
  data* end()const
  {
    return NULL;
  }
  size_t count()const
  {
      return _count;
  }

  struct iterator
  {
  private:
    data *element;
    iterator operator++(int size);
  public:
    iterator(data* t)
    {
      element=t;
    }
    iterator operator=(data* t)
    {
      element=t;
    }
    data* operator++()
    {
      if(element)
      return element=element->next;
      return NULL;
    }
    operator data*()
    {
       return element;
    }
    T operator*()
    {
      return element->t;
    }
  };
};
#endif

你可能感兴趣的:(list,null,delete,iterator,insert)