练习C/C++基本功(列表和迭代器的实现和测试)

Posted zsl6658

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习C/C++基本功(列表和迭代器的实现和测试)相关的知识,希望对你有一定的参考价值。

                                          练习C/C++基本功(列表和迭代器的实现和测试)

#include "stdafx.h"
#include <iostream>
#include "stdlib.h"
#include "stdio.h"
#include "time.h"


using namespace std;
long DEFAULT_LIST_CAPACITY = 30;

template <class Item> class List
{
public:
  List ( long size = DEFAULT_LIST_CAPACITY )
  {
      arr=new Item[size]; 
      this->count=0;
  }

  List ( List& ls )
  {
      if(ls.arr)
          delete arr;
      arr=new Item[ls.count];
      for(int i=0;i<ls.count;i++)
          arr[i]=ls.arr[i];
      this->count=ls.count;
  }  
  
  ~List ( )
  {
      if(arr)
         delete []arr;
      arr=NULL;
  }

  List operator = ( const List& ls )
  {
      if(this=&ls)
         return *this;
      if(ls.arr)
         delete arr;
      arr=new Item[ls.count];
      for(int i=0;i<ls.count;i++)
         arr[i]=ls.arr[i];
      this->count=ls.count;
  }

  long Count ( ) const
  {
      return this->count;
  }

  Item& Get ( long index ) const
  {
       return arr[index]; 
  }
  
  Item& First ( ) const
  {
       return arr[0];
  }
  
  Item& Last ( ) const
  {
       return arr[this->count-1];
  }

  bool Includes ( const Item& item) const
  {
       for(int i=0;i<this->count;i++)
          if(item==arr[i])
              return true;
       return false;
  }
        
  void Append ( const Item& item)
  {
       Item* temp=new Item[this->count+1];
       for(int i=0;i<this->count;i++)
           temp[i]=arr[i];
       temp[i+1]=item;
       delete []arr;
       arr=temp;
       this->count++;
  }
  
  void Prepend ( const Item& item)
  {
       Item* temp=new Item[this->count+1];
       for(int i=1;i<this->count+1;i++)
          temp[i]=arr[i];
       temp[0]=item;
       delete []arr;
       arr=temp;
       this->count++;
  }

  void Remove ( const Item& item)
  {
       Item* temp=new Item[count-1];
       for(int i=0;i<this->count;i++)
          if(item==arr[i])
          {
              i++;
          }
          else
          {
              temp[i]=arr[i];
          }
       delete []arr;
       arr=temp;
       this->count--;
  }
  
  void RemoveLast ( const Item& )
  {
      Item* temp=new Item[count-1];
      for(int i=0;i<count-1;i++)
          temp[i]=arr[i];
      count--;
  }

  void RemoveFisrt ( const Item& )
  {
      Item* temp=new Item[count-1];
      for(int i=0;i<count-1;i++)
         temp[i]=arr[i+1];
      count--;
  }

  void RemoveAll ( const Item& )
  {
      delete []arr;
      arr=NULL;
      count=0;
  }

  Item& Top ( ) const
  {
      return arr[0];
  }

  void Push ( const Item& item )
  {
      Item* temp=new Item[count+1];
      for(int i=0;i<count;i++)
         temp[i]=arr[i];
      temp[i+1]=item;
      count++;
  }

  Item& Pop ()
  {
      Item* temp=new Item[count-1];
      Item item=arr[0];
      for(int i=0;i<count-1;i++)
          temp[i]=arr[i+1];
      delete arr;
      arr=temp;
      return item;
  }

private:
  Item* arr;
  int count;
};

template <class Item> class Iterator 
{
public:
            virtual void First ( ) = 0;
            virtual void Next ( ) = 0;
            virtual bool IsDone ( ) const = 0;
            virtual Item CurrentItem ( ) const = 0;
protected:
   Iterator ( ) {}
};

template <class Item> class ListIterator : public Iterator<Item> 
{
public:
    ListIterator(const List<Item>* aList);
    virtual void First();
    virtual void Next();
    virtual bool IsDone() const;
    virtual Item CurrentItem() const;
private:
    const List<Item>* _list;
    long _current;
};

template <class Item>
ListIterator<Item>::ListIterator ( const List<Item>* aList ) : _list(aList), _current(0) { }

template <class Item>
void ListIterator<Item>::First () { _current = 0; }

template <class Item>
void ListIterator<Item>::Next () 
{
        _current++;
}

template <class Item>
bool ListIterator<Item>::IsDone () const { return _current >= _list->Count(); }

template <class Item>
Item ListIterator<Item>::CurrentItem () const 
{
    if (IsDone()) 
        {
        throw "IteratorOutOfBounds";
    }
    return _list->Get(_current);
}

class Item
{
public:
 Item():a(0)
 {

 }

 Item(int a)
 {
     this->a=a;
 }

 bool operator!=(const Item& item)
 {
     if(this->a!=item.a)
         return true;
     else
         return false;
 }

friend ostream& operator<<(ostream& os,const Item& item)
 {
     os<<item.a<<" ";
     return os;
 }
private:
   int a;
};

int main(int argc, char* argv[])
{
 
     List<Item>* ls=new List<Item>(10);
     ListIterator<Item> itor(ls);
     for(int i=0;i<10;i++)
     {
         Item item(i);
         ls->Prepend(item);
     }
     while(!itor.IsDone())
     {
         cout<<itor.CurrentItem();
         itor.Next();
     }

     return 0;
}


 

以上是关于练习C/C++基本功(列表和迭代器的实现和测试)的主要内容,如果未能解决你的问题,请参考以下文章

[C/C++]详解STL容器3--list的功能和模拟实现(迭代器失效问题)

C++ 迭代器iterator的实现原理

跟你深入剖析可迭代对象和迭代器的区别和联系

跟你深入剖析可迭代对象和迭代器的区别和联系

C/C++——set的基本操作总结

Python中的“迭代”详解