#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;
}
原文链接: https://www.cnblogs.com/Azhu/archive/2012/07/11/2586394.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/54934
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!