#include<iostream.h>
template<class T>
class node
{
public:
T data;
void * pre;
void * next;
};
template<class T>
class iter
{
public:
typedef node<T> * lnode;
lnode pnode;
iter(iter<T>& a):pnode(a.pnode){}
iter(){}
iter(lnode a):pnode(a){}
T & operator *() const {return (pnode->data);}
T * operator ->()const {return &(operator *());}
bool operator !=(iter<T> & a )const {return pnode != a.pnode; }
bool operator ==(iter<T> & a )const {return pnode == a.pnode; }
iter<T> & operator++() {pnode=(lnode)pnode->next; return *this;}
iter<T> & operator--() {pnode=(lnode)pnode->pre; return *this;}
iter<T> operator--(int) {iter<T> tmp=*this; --*this;return tmp;}
iter<T> operator++(int) {iter<T> tmp=*this; ++*this;return tmp;}
};
template<class T>
class test
{
public:
typedef iter<T> iterator;
typedef node<T> * lp;
public:
test(T a);
test();
~test();
iterator first();
iterator end();
void insert(T a);
void clear();
private:
lp p;
};
template<class T>
test<T>::test(T a)
{
lp tmp=new node<T>;
tmp->data=a;
tmp->pre=NULL;
tmp->next=p;
node<T> *tmpp=(node<T> *)p->pre;
tmpp->next=tmp;
}
template<class T>
test<T>::test()
{
p= new node<T>;
p->pre=p;
p->next=p;
}
template<class T>
test<T>::~test()
{
delete p;
}
template<class T>
void test<T>::clear()
{
lp tmp=NULL;
lp root=p;
while(p->next != root)
{
tmp=(lp)p->next;
delete p;
p=tmp;
}
}
template<class T>
void test<T>::insert(T a)
{
node<T> *tmp=new node<T>;
tmp->data=a;
tmp->pre=NULL;
tmp->next=p;
node<T> *tmpp=(node<T> *)p->pre;
tmpp->next=tmp;
p->pre=tmp;
}
template<class T>
typename test<T>::iterator test<T>::first()
{
return (lp)p->next;
}
template<class T>
typename test<T>::iterator test<T>::end()
{
return p;
}
int main()
{
test<int> a;
a.insert(99);
a.insert(10);
a.insert(93);
a.insert(29);
a.insert(59);
a.insert(69);
a.insert(79);
a.insert(66);
a.insert(340);
a.insert(0);
a.insert(330);
test<int>::iterator i,ii;
i=a.first();
ii=a.end();
while(i != ii)
{
cout<<*i<<endl;
i++;
}
a.clear();
return 0;
}
template<class T>
class node
{
public:
T data;
void * pre;
void * next;
};
template<class T>
class iter
{
public:
typedef node<T> * lnode;
lnode pnode;
iter(iter<T>& a):pnode(a.pnode){}
iter(){}
iter(lnode a):pnode(a){}
T & operator *() const {return (pnode->data);}
T * operator ->()const {return &(operator *());}
bool operator !=(iter<T> & a )const {return pnode != a.pnode; }
bool operator ==(iter<T> & a )const {return pnode == a.pnode; }
iter<T> & operator++() {pnode=(lnode)pnode->next; return *this;}
iter<T> & operator--() {pnode=(lnode)pnode->pre; return *this;}
iter<T> operator--(int) {iter<T> tmp=*this; --*this;return tmp;}
iter<T> operator++(int) {iter<T> tmp=*this; ++*this;return tmp;}
};
template<class T>
class test
{
public:
typedef iter<T> iterator;
typedef node<T> * lp;
public:
test(T a);
test();
~test();
iterator first();
iterator end();
void insert(T a);
void clear();
private:
lp p;
};
template<class T>
test<T>::test(T a)
{
lp tmp=new node<T>;
tmp->data=a;
tmp->pre=NULL;
tmp->next=p;
node<T> *tmpp=(node<T> *)p->pre;
tmpp->next=tmp;
}
template<class T>
test<T>::test()
{
p= new node<T>;
p->pre=p;
p->next=p;
}
template<class T>
test<T>::~test()
{
delete p;
}
template<class T>
void test<T>::clear()
{
lp tmp=NULL;
lp root=p;
while(p->next != root)
{
tmp=(lp)p->next;
delete p;
p=tmp;
}
}
template<class T>
void test<T>::insert(T a)
{
node<T> *tmp=new node<T>;
tmp->data=a;
tmp->pre=NULL;
tmp->next=p;
node<T> *tmpp=(node<T> *)p->pre;
tmpp->next=tmp;
p->pre=tmp;
}
template<class T>
typename test<T>::iterator test<T>::first()
{
return (lp)p->next;
}
template<class T>
typename test<T>::iterator test<T>::end()
{
return p;
}
int main()
{
test<int> a;
a.insert(99);
a.insert(10);
a.insert(93);
a.insert(29);
a.insert(59);
a.insert(69);
a.insert(79);
a.insert(66);
a.insert(340);
a.insert(0);
a.insert(330);
test<int>::iterator i,ii;
i=a.first();
ii=a.end();
while(i != ii)
{
cout<<*i<<endl;
i++;
}
a.clear();
return 0;
}