ListNode.h
template<typename Type> class DoubleList;
template<typename Type> class ListNode
{
private:
friend class DoubleList<Type>;
ListNode(): prior(NULL), next(NULL) {}
ListNode(Type item, ListNode<Type> *p=NULL, ListNode<Type> *n = NULL):
data(item), prior(p), next(n){}
~ListNode()
{
prior = NULL;
next = NULL;
}
public:
Type GetData();
friend ostream& operator<< <Type>(ostream& ,ListNode<Type>& );
private:
Type data;
ListNode<Type> *prior;
ListNode<Type> *next;
};
template<typename Type> ostream& operator<<(ostream& os,ListNode<Type>& out){
os<<out.data;
return os;
}
template<typename Type>
Type ListNode<Type>::GetData()
{
return this->data;
}
DoubleList.h
#include "ListNode.h"
template<typename Type> class DoubleList
{
public:
DoubleList():head( new ListNode<Type>() )
{
head->prior = head;
head->next = head;
}
~DoubleList()
{
MakeEmpty();
delete head;
}
public:
void MakeEmpty();
int Length();
ListNode<Type> *Find( int n=0);
ListNode<Type> *FindData(Type item);
bool Insert(Type item, int n=0);
Type Remove(int n=0);
Type Get(int n=0);
void Print();
private:
ListNode<Type> *head;
};
template<typename Type>
void DoubleList<Type>::MakeEmpty()
{
ListNode<Type> *pmov = head->next;
ListNode<Type> *pdel;
while(pmov != head )
{
pdel = pmov;
pmov =pmov->next;
delete pdel;
}
head->next = head;
head->prior = head;
}
template<typename Type>
int DoubleList<Type>::Length()
{
int count=0;
ListNode<Type> *pr = head->next;
ListNode<Type> *pl = head->prior;
while(1)
{
if(pl->next == pr)
break;
if( pl == pr && pr != head)
{
count++;
break;
}
count+=2;
pr = pr->next;
pl = pl->prior;
}
return count;
}
template<typename Type>
ListNode<Type>* DoubleList<Type>::Find(int n=0)
{
if(n<1)
{
cout<<"the n is illegal"<<endl;
return NULL;
}
ListNode<Type> *pmov =head;
for(int i=0; i<n;i++)
{
pmov = pmov->next;
if(pmov == head)
{
cout<<"the n is out of the boundary"<<endl;
return NULL;
}
}
return pmov;
}
template<typename Type>
ListNode<Type>* DoubleList<Type>::FindData(Type item)
{
ListNode<Type> *pright = head->next;
ListNode<Type> *pleft = head->prior;
while(1)
{
if(pleft->next == pright)
return NULL;
if(pleft == pright && pright != head)
{
if( pright->data == item )
return pright;
else
return NULL;
}
if(pright->data == item )
return pright;
if(pleft->data == item )
return pleft;
pright= pright->next;
pleft = pleft->prior;
}
}
template<typename Type>
bool DoubleList<Type>::Insert( Type item, int n=0)
{
if(n<0)
{
cout<<"the n is illegal"<<endl;
return false;
}
if(n == 0)
{
ListNode<Type> *pinsert = new ListNode<Type>(item);
if(head->next == head)
{
pinsert->next =head;
pinsert->prior = head;
head->next = pinsert;
head->prior = pinsert;
}
else
{
pinsert->next =head->next;
head->next->prior =pinsert;
head->next = pinsert;
pinsert->prior = head;
}
return true;
}
ListNode<Type> *p =Find(n);
if(p == NULL)
{
cout<<"can not insert the data"<<endl;
return false;
}
ListNode<Type> *pinsert = new ListNode<Type>(item);
pinsert->next = p->next;
p->next->prior = pinsert;
p->next = pinsert;
pinsert->prior= p;
return true;
}
template<typename Type>
Type DoubleList<Type>::Remove(int n=0)
{
if(n<1)
{
cout<<"the n is illegal"<<endl;
return 0;
}
ListNode<Type> *pdel =Find(n);
if(pdel == NULL)
{
cout<<"cannot remove the node at "<<n<<endl;
return 0;
}
Type item;
if( head->next->next == head)
{
head->next=head;
head->prior=head;
item = pdel->data;
delete pdel;
return item;
}
pdel->prior->next = pdel->next;
pdel->next->prior =pdel->prior;
item = pdel->data;
delete pdel;
return item;
}
template<typename Type>
Type DoubleList<Type>::Get(int n=0)
{
if(n<1)
{
cout<<"the n is illegal"<<endl;
return 0;
}
ListNode<Type> *p = Find(n);
if(p == NULL)
{
cout<<"can not get the data at the "<<n<<" node"<<endl;
return 0;
}
return p->data;
}
template<typename Type>
void DoubleList<Type>::Print()
{
ListNode<Type> *pmov =head->next;
cout<<"head-->";
while(pmov != head)
{
cout<<pmov->data<<"-->";
pmov = pmov->next;
}
cout<<"over"<<endl;
}