您的位置:首页 > 理论基础 > 数据结构算法

数据结构之单链表C++(模板)

2013-05-04 22:17 447 查看
#ifndef STL_LINK_LIST
#define STL_LINK_LIST

/************************************************************************/
/* 以下为C++实现单链表	(STL模板)
/************************************************************************/

/************************************************************************/
/* //结点定义
/************************************************************************/

template <class Elemplent>
class CNode
{
public:
Elemplent data;
CNode<Elemplent> *next;
public:
CNode<Elemplent> ();
CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext = NULL);
~CNode();
};

//结点构造函数
template<class Elemplent>
CNode<Elemplent>::CNode()
{
next = NULL;
}

//结点析构函数
template<class Elemplent>
CNode<Elemplent>::~CNode()
{

}

//创建新节点函数
template<class Elemplent>
CNode<Elemplent>::CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext )
{
data = tempElemplent;
next = tempNext;
}

/************************************************************************/
/* //单链表
/************************************************************************/
template<class Elemplent>
class CList
{
protected:
CNode<Elemplent> *head;
int CountTotal;
public:
CList();
CList(const CList<Elemplent> & temp);
~CList();
void initClist();
CNode<Elemplent>* GetPtrWithPosition(int position  = 0) const;
public:
bool IsEmpty();
int GetLength();
bool Insert(const Elemplent &tempElemplent,int position = 0);
bool Delete(int position  = 0);
bool SetElemplentWithPosition(const Elemplent &tempElemplent,int position = 0);
bool GetElemplentForElemplent(Elemplent &tempElemplent,int position = 0) const;
bool Clear();
public:
CList<Elemplent>&  operator = (const CList<Elemplent> & temp);

};

//单链表构造函数
template<class Elemplent>
CList<Elemplent>::CList()
{
initClist();
}

//单链表析构函数
template<class Elemplent>
CList<Elemplent>::~CList()
{
Clear();
delete head;
}

//单链表赋值构造函数
template<class Elemplent>
CList<Elemplent>::CList(const CList<Elemplent> & temp)
{
int lenth = temp.GetLength;
Elemplent tempplent;
initClist();//刚刚忘记初始化了!
for (int cocypostion = 1;cocypostion<lenth;cocypostion++)
{
temp.GetElemplentForElemplent(tempplent,cocypostion);
Insert(tempplent,cocypostion);
}
}

//单链表赋值操作重载
template<class Elemplent>
CList<Elemplent>& CList<Elemplent>::operator=(const CList<Elemplent> & temp)
{
if (&temp == this)
{
return NULL;
}
int length=temp.GetLength;
Elemplent tempElemplent;
Clear();
for (int copyposition = 1 ;copyposition<length;copyposition++)
{
temp.GetElemplentForElemplent(tempElemplent,copyposition);
Insert(tempElemplent,copyposition);
}
return *this;
}

//单链表初始化单链表
template<class Elemplent>
void CList<Elemplent>::initClist()
{
head = new CNode();
CountTotal = 0;
}

//单链表判断是否为空表
template<class Elemplent>
bool CList<Elemplent>::IsEmpty()
{
return NULL==head->next;
}

//单链表用位置返回元素的指针
template <class Elemplent>
CNode<Elemplent>* CList<Elemplent>::GetPtrWithPosition(int position /* = 0 */)const
{
int temPositionCursor = 0;
CNode<Elemplent> *tempCursorPtr;
tempCursorPtr = head;
if (position>0||position<CountTotal)
{
return NULL;
}
else
{
while(temPositionCursor<=CountTotal && tempCursorPtr !=NULL)
{
if (temPositionCursor == position)
{
break;
}
temPositionCursor++;
tempCursorPtr= tempCursorPtr->next;
}
return tempCursorPtr;
}

}

//单链表得到链表长度
template <class Elemplent>
int CList<Elemplent>::GetLength()
{
return CountTotal;
}

//单链表插入一个元素
template <class Elemplent>
bool CList<Elemplent>::Insert(const Elemplent &tempElemplent,int position /* = 0 */)
{
CNode<Elemplent> *tempCursorPtr;
if ( position<0||position>CountTotal)
{
return false;
}
tempCursorPtr = GetPtrWithPosition(position-1);
CNode<Elemplent> *newtempNode = new CNode(tempElemplent,tempCursorPtr->next);
tempCursorPtr->next = tempElemplent;
CountTotal ++;
return true;
}

//单链表用位置设这某个元素的值
template <class Elemplent>
bool CList<Elemplent>::SetElemplentWithPosition(const Elemplent &tempElemplent,int position /* = 0 */)
{
CNode<Elemplent> *tempCursorPtr;
if ( position<0||position>CountTotal)
{
return false;
}
tempCursorPtr = GetPtrWithPosition(position);
tempCursorPtr->data = tempElemplent;
return true;
}

//单链表得到某个位置的元素的值
template<class Elemplent>
bool CList<Elemplent>::GetElemplentForElemplent(Elemplent &tempElemplent,int position /* = 0 */)const
{
CNode<Elemplent> *tempCursorPtr;
if ( position<0||position>CountTotal)
{
return false;
}
tempCursorPtr = GetPtrWithPosition(position);
tempElemplent = tempCursorPtr->data;
return true;

}

//单链表删除一个元素
template<class Elemplent>
bool CList<Elemplent>::Delete(int position /* = 0 */)
{
if (position<0||position>CountTotal)
{
return false;
}
CNode<Elemplent> *tempElemplent;
tempElemplent = GetPtrWithPosition(position-1);
CNode<Elemplent> *deltePtr = tempElemplent->next;
tempElemplent->next = deltePtr->next;
delete deltePtr;
CountTotal--;
return true;
}

//单链表清空链表
template <class Elemplent>
bool CList<Elemplent>::Clear()
{
while(GetLength()>0)
{
Delete(1);
}
return IsEmpty()== true;
}

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: