#include <string.h>
template <class T>
struct SNode
{
T data;
SNode<T>* pPrev;
SNode<T>* pNext;
};
template <class T>
typedef void* POSITION;
template <class T>
class CList
{
public:
CList();
return m_pHead->data;
return m_pTail->data;
}
T RemoveHead(); //删除头节点 返回头节点的数据
T RemoveTail( );//删除尾节点 返回尾节点的数据
void AddHead( const T& data); //头部添加
void AddTail(const T& data); //尾部添加
void RemoveAll( ); //全部删除
POSITION GetHeadPosition( ) const //获取头节点
{
return m_pHead;
return m_pTail;
T GetAt(POSITION pos) const //取得当前节点的值
{
return ((SNode<T>*)pos)->data;
}
void SetAt(POSITION pos,T data) //设置当前节点的值
{
pos->data=data;
POSITION InsertAfter( POSITION pos ,T data);//在指定节点的后面进行插入一个节点
POSITION Find( T data, POSITION startAfter = NULL) const;//从指定的这个结点开始往后查,如果查找到,即返回该结点指针,否则返回NULL
POSITION FindIndex( int nIndex ) const;返回第nIndex的结点指针
int GetCount() const//链表的长度
{
return m_nCount;
}
bool IsEmpty( ) const //判断链表是否为空
{
return (m_nCount==0);
}
private:
SNode<T>* m_pHead;//链表的头节点
SNode<T>* m_pTail;//链表的尾节点
int m_nCount; //链表节点的个数
};
template <class T>
CList<T>::CList()//初始化链表
{
m_pHead=NULL;
m_pTail=NULL;
m_nCount=0;
}
template <class T>
CList<T>::~CList() //链表清空
{
RemoveAll();
}
template <class T>
T CList<T>::RemoveHead()
{
T data=m_pHead->data;
POSITION p=m_pHead;
m_pHead =m_pHead->pNext;
if(m_pHead) //链表头结点的前驱为空
m_pHead->pPrev=NULL;
else //判断是否为空链表
m_pTail=NULL;
delete p;
p=NULL;
m_nCount--;
return data;
}
template <class T>
T CList<T>::RemoveTail( )
{
DATA data=m_pTail->data;
POSITION p=m_pTail;
m_pTail=m_pTail->pPrev;
if(m_pTail) //链表尾节点的后继为空
m_pTail->pNext=NULL;
else //判断链表是否为空
m_pHead=NULL;
delete p;
p =NULL;
m_nCount--;
return data;
}
template <class T>
void CList<T>::AddHead( const T& data)
{
SNode<T>* p=new SNode<T>;
p->data=data;
p->pPrev=NULL;
p->pNext=m_pHead;
if(m_pHead) //判断是否为空链表
m_pHead->pPrev=p;
else //空链表 给尾结点赋值
m_pTail=p;
m_pHead=p;
m_nCount++; //链表节点的个数加1
}
template <class T>
void CList<T>::AddTail(const T& data)
{
SNode<T>* p=new SNode<T>;
p->data=data;
p->pNext=NULL;
p->pPrev=m_pTail;
if(m_pTail) //判断是否为空链表
m_pTail->pNext=p;
else 空链表 给头结点赋值
m_pHead=p;
m_pTail=p;
m_nCount++ //链表节点的个数加1
}
template <class T>
void CList<T>::RemoveAll( )
{
POSITION p=m_pHead;
while(m_pHead)
{
m_pHead=m_pHead->pNext;
delete p;
p=m_pHead;
}
m_nCount=0; //把链表的内容清空
m_pHead=NULL;
m_pTail=NULL;
}
template <class T>
T CList<T>::GetNext( POSITION& pos)
{
DATA data=((SNode<T>*)pos)->data;
pos=((SNode<T>*)pos)->pNext;
return data;
}
template <class T>
T CList<T>::GetPrev(POSITION& pos)
{
T data=((SNode<T>*)pos)->data;
pos=((SNode<T>*)pos)->pPrev;
return data;
}
template <class T>
void CList<T>::RemoveAt( POSITION pos)
{
if(((SNode<T>*)pos)->pPrev==NULL) //节点的前驱为空 节点为头结点
{
m_pHead=((SNode<T>*)pos)->pNext;
}
else
{
((SNode<T>*)pos)->pPrev->pNext=((SNode<T>*)pos)->pNext;
}
if(((SNode<T>*)pos)->pNext==NULL) //节点的后继为空 节点为尾节点
{
m_pTail=((SNode<T>*)pos)->pPrev;
}
else
{
((SNode<T>*)pos)->pNext->pPrev=((SNode<T>*)pos)->pPrev;
}
delete pos;
pos=NULL;
m_nCount--;
}
template <class T>
POSITION CList<T>::InsertBefore(POSITION pos, T data)
{
POSITION p =new SNode;
p->data=data;
p->pPrev=pos->pPrev;
if(pos->pPrev) // 需要判断节点是否为头结点
pos->pPrev->pNext=p;
else
m_pHead=p;
pos->pPrev=p;
p->pNext=pos;
m_nCount++;
return p;
}
template <class T>
POSITION CList<T>::InsertAfter( POSITION pos ,T data)
{
POSITION p=new SNode;
p->data = data;
p->pNext=pos->pNext;
if(pos->pNext) //需要判断节点是否为尾节点
pos->pNext->pPrev=p;
else
m_pTail=p;
pos->pNext=p;
p->pPrev=pos;
m_nCount++;
return p;
POSITION CList<T>::Find(T data, POSITION startAfter) const
{
POSITION p;
if(startAfter==NULL)
{
p=m_pHead;
}
else
{
p= ((SNode<T>*)startAfter)->pNext;
}
for(;p!=NULL;p=((SNode<T>*)p)->pNext)
{
if(memcmp(&data,&((SNode<T>*)p)->data,sizeof(data))==0) //比较是否相等
return p;
}
return NULL;
}
template <class T>
POSITION CList<T>::FindIndex( int nIndex ) const
{
if(nIndex < 0 || nIndex > m_nCount) //需要判断数值nIndex 不能超出范围
{
return NULL;
}
POSITION p=m_pHead;
while(nIndex--)
{
if(!p)
return NULL;
p=p->pNext;
}
return p;
}
template <class T>
struct SNode
{
T data;
SNode<T>* pPrev;
SNode<T>* pNext;
};
template <class T>
typedef void* POSITION;
template <class T>
class CList
{
public:
CList();
virtual ~CList();
T GetHead( ) const //获取头节点的数据
{return m_pHead->data;
}
T GetTail() const //获取尾节点的数据
{return m_pTail->data;
}
T RemoveHead(); //删除头节点 返回头节点的数据
T RemoveTail( );//删除尾节点 返回尾节点的数据
void AddHead( const T& data); //头部添加
void AddTail(const T& data); //尾部添加
void RemoveAll( ); //全部删除
POSITION GetHeadPosition( ) const //获取头节点
{
return m_pHead;
}
POSITION GetTailPosition( ) const //获取尾节点
{return m_pTail;
}
T GetNext( POSITION& pos);//取得当前节点的值 pos向后移动
T GetPrev(POSITION& pos);//取得当前节点的值 pos向前移动T GetAt(POSITION pos) const //取得当前节点的值
{
return ((SNode<T>*)pos)->data;
}
void SetAt(POSITION pos,T data) //设置当前节点的值
{
pos->data=data;
}
void RemoveAt( POSITION pos);//删除当前节点
POSITION InsertBefore(POSITION pos, T data);//在指定结点的前面进行插入一个节点POSITION InsertAfter( POSITION pos ,T data);//在指定节点的后面进行插入一个节点
POSITION Find( T data, POSITION startAfter = NULL) const;//从指定的这个结点开始往后查,如果查找到,即返回该结点指针,否则返回NULL
POSITION FindIndex( int nIndex ) const;返回第nIndex的结点指针
int GetCount() const//链表的长度
{
return m_nCount;
}
bool IsEmpty( ) const //判断链表是否为空
{
return (m_nCount==0);
}
private:
SNode<T>* m_pHead;//链表的头节点
SNode<T>* m_pTail;//链表的尾节点
int m_nCount; //链表节点的个数
};
template <class T>
CList<T>::CList()//初始化链表
{
m_pHead=NULL;
m_pTail=NULL;
m_nCount=0;
}
template <class T>
CList<T>::~CList() //链表清空
{
RemoveAll();
}
template <class T>
T CList<T>::RemoveHead()
{
T data=m_pHead->data;
POSITION p=m_pHead;
m_pHead =m_pHead->pNext;
if(m_pHead) //链表头结点的前驱为空
m_pHead->pPrev=NULL;
else //判断是否为空链表
m_pTail=NULL;
delete p;
p=NULL;
m_nCount--;
return data;
}
template <class T>
T CList<T>::RemoveTail( )
{
DATA data=m_pTail->data;
POSITION p=m_pTail;
m_pTail=m_pTail->pPrev;
if(m_pTail) //链表尾节点的后继为空
m_pTail->pNext=NULL;
else //判断链表是否为空
m_pHead=NULL;
delete p;
p =NULL;
m_nCount--;
return data;
}
template <class T>
void CList<T>::AddHead( const T& data)
{
SNode<T>* p=new SNode<T>;
p->data=data;
p->pPrev=NULL;
p->pNext=m_pHead;
if(m_pHead) //判断是否为空链表
m_pHead->pPrev=p;
else //空链表 给尾结点赋值
m_pTail=p;
m_pHead=p;
m_nCount++; //链表节点的个数加1
}
template <class T>
void CList<T>::AddTail(const T& data)
{
SNode<T>* p=new SNode<T>;
p->data=data;
p->pNext=NULL;
p->pPrev=m_pTail;
if(m_pTail) //判断是否为空链表
m_pTail->pNext=p;
else 空链表 给头结点赋值
m_pHead=p;
m_pTail=p;
m_nCount++ //链表节点的个数加1
}
template <class T>
void CList<T>::RemoveAll( )
{
POSITION p=m_pHead;
while(m_pHead)
{
m_pHead=m_pHead->pNext;
delete p;
p=m_pHead;
}
m_nCount=0; //把链表的内容清空
m_pHead=NULL;
m_pTail=NULL;
}
template <class T>
T CList<T>::GetNext( POSITION& pos)
{
DATA data=((SNode<T>*)pos)->data;
pos=((SNode<T>*)pos)->pNext;
return data;
}
template <class T>
T CList<T>::GetPrev(POSITION& pos)
{
T data=((SNode<T>*)pos)->data;
pos=((SNode<T>*)pos)->pPrev;
return data;
}
template <class T>
void CList<T>::RemoveAt( POSITION pos)
{
if(((SNode<T>*)pos)->pPrev==NULL) //节点的前驱为空 节点为头结点
{
m_pHead=((SNode<T>*)pos)->pNext;
}
else
{
((SNode<T>*)pos)->pPrev->pNext=((SNode<T>*)pos)->pNext;
}
if(((SNode<T>*)pos)->pNext==NULL) //节点的后继为空 节点为尾节点
{
m_pTail=((SNode<T>*)pos)->pPrev;
}
else
{
((SNode<T>*)pos)->pNext->pPrev=((SNode<T>*)pos)->pPrev;
}
delete pos;
pos=NULL;
m_nCount--;
}
template <class T>
POSITION CList<T>::InsertBefore(POSITION pos, T data)
{
POSITION p =new SNode;
p->data=data;
p->pPrev=pos->pPrev;
if(pos->pPrev) // 需要判断节点是否为头结点
pos->pPrev->pNext=p;
else
m_pHead=p;
pos->pPrev=p;
p->pNext=pos;
m_nCount++;
return p;
}
template <class T>
POSITION CList<T>::InsertAfter( POSITION pos ,T data)
{
POSITION p=new SNode;
p->data = data;
p->pNext=pos->pNext;
if(pos->pNext) //需要判断节点是否为尾节点
pos->pNext->pPrev=p;
else
m_pTail=p;
pos->pNext=p;
p->pPrev=pos;
m_nCount++;
return p;
}
POSITION CList<T>::Find(T data, POSITION startAfter) const
{
POSITION p;
if(startAfter==NULL)
{
p=m_pHead;
}
else
{
p= ((SNode<T>*)startAfter)->pNext;
}
for(;p!=NULL;p=((SNode<T>*)p)->pNext)
{
if(memcmp(&data,&((SNode<T>*)p)->data,sizeof(data))==0) //比较是否相等
return p;
}
return NULL;
}
template <class T>
POSITION CList<T>::FindIndex( int nIndex ) const
{
if(nIndex < 0 || nIndex > m_nCount) //需要判断数值nIndex 不能超出范围
{
return NULL;
}
POSITION p=m_pHead;
while(nIndex--)
{
if(!p)
return NULL;
p=p->pNext;
}
return p;
}