#include<iostream>
using namespace std;

template<class T>
struct Unit
{
    public:
    Unit * next;
    T value;
};
template<class T>
class MyLink
{
public:

    class LinkIterator
    {
        private:
        Unit<T> * init;
        public:
        LinkIterator(Unit<T> * init)
        {
            this->init=init;
        }
        bool operator !=(LinkIterator& it)
        {
            return this->init!=it.init;
        }
        void operator ++(int)
        {
            this->init=init->next;
        }
        Unit<T> operator *()
        {
            return *init;
        }
    };
private:
    Unit<T> *head;
    Unit<T> *prev;
    Unit<T> *hail;
public:
    MyLink()
    {
        head=prev=hail=NULL;
    }
    void Add(T value)
    {
        Unit<T>* unit=new Unit<T>();
        unit->value=value;
        unit->next=NULL;
        if(NULL==head)
        {
            head=prev=unit;
        }
        else
        {
            prev->next=unit;
            prev=unit;
        }
        hail=unit->next;
    }
    Unit<T> *Begin()
    {
        return head;
    }
    Unit<T> *End()
    {
        return hail;
    }
    virtual ~MyLink()
    {
        prev=head;
        Unit<T>* next;
        while(NULL!=prev)
        {
            next=prev->next;
            delete prev;
            prev=next;
        }
    }
};

template<class T>
ostream& operator<< (ostream& os, const Unit<T>& s)
//全局重载操作符
{
    os<<s.value;
    return os;
}

template<class T>
void display (T begin, T end)
{
    for(T mid=begin;mid!=end; mid ++)
        cout<<*mid<<" ";
    cout<<endl;
}

int main()
{
    MyLink<int> ml;
    for(int i=0;i<5;i++)
        ml.Add(i+1);
    MyLink<int>:: LinkIterator start=ml.Begin();
    MyLink<int>:: LinkIterator end=ml.End();
    display(start,end);
    return 0;
}

相关文章:

  • 2021-07-01
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2022-01-01
猜你喜欢
  • 2021-06-13
  • 2022-01-19
  • 2021-12-09
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案