#include <iostream.h>
template<typename T>
struct DLNode
{
T data;
DLNode<T> *prior,*next;
};
template<typename T>
class DLinkList
{
private:
DLNode<T> *Head;
DLNode<T>* GetElemP(int i)const
{
int j=0;
DLNode<T> *p=Head;
if(i<0)
return NULL;
if(i==0)
return p;
do
{
p=p->next;
j++;
}while(p!=Head && j<i);
if(p==Head)
return NULL;
else
return p;
}
public:
DLinkList()
{
Head=new DLNode<T>;
Head->next=Head->prior=Head;
}
bool ListInsert(int i, T e)
{
DLNode<T> *s, *p=GetElemP(i-1);
if(p==NULL)
return false;
s=new DLNode<T>;
if(s==NULL)
return false;
s->data=e;
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
return true;
}
void ListTraverse(void(*visit)(T*))const
{
DLNode<T> *p=Head->next;
while(p!=Head)
{
visit(&p->data);
p=p->next;
}
cout<<endl;
}
};
template<typename T>
inline void print(T* c)
{
cout<<*c<<" ";
}
typedef int T;
int main()
{
DLinkList<T> L;
T e;
int i, n=4;
bool j;
for(i=1; i<=5; i++)
L.ListInsert(i, i);
cout<<"在L的表尾依次插入1~5后,L=";
L.ListTraverse(print);
return 0;
}
template<typename T>
struct DLNode
{
T data;
DLNode<T> *prior,*next;
};
template<typename T>
class DLinkList
{
private:
DLNode<T> *Head;
DLNode<T>* GetElemP(int i)const
{
int j=0;
DLNode<T> *p=Head;
if(i<0)
return NULL;
if(i==0)
return p;
do
{
p=p->next;
j++;
}while(p!=Head && j<i);
if(p==Head)
return NULL;
else
return p;
}
public:
DLinkList()
{
Head=new DLNode<T>;
Head->next=Head->prior=Head;
}
bool ListInsert(int i, T e)
{
DLNode<T> *s, *p=GetElemP(i-1);
if(p==NULL)
return false;
s=new DLNode<T>;
if(s==NULL)
return false;
s->data=e;
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
return true;
}
void ListTraverse(void(*visit)(T*))const
{
DLNode<T> *p=Head->next;
while(p!=Head)
{
visit(&p->data);
p=p->next;
}
cout<<endl;
}
};
template<typename T>
inline void print(T* c)
{
cout<<*c<<" ";
}
typedef int T;
int main()
{
DLinkList<T> L;
T e;
int i, n=4;
bool j;
for(i=1; i<=5; i++)
L.ListInsert(i, i);
cout<<"在L的表尾依次插入1~5后,L=";
L.ListTraverse(print);
return 0;
}