#ifndef STL_LINK_CIRCLELIST_H
#define STL_LINK_CIRCLELIST_H
/************************************************************************/
/* 以下是单循环链表C++实现
/************************************************************************/
/************************************************************************/
/* 节点类
/************************************************************************/
//结点定义
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 CircleList
{
protected:
CNode<Elemplent> *head;
CNode<Elemplent> *CursorPtr;
int CursorPosition;
int CountTotal;
public:
CircleList();
~CircleList();
void InitCircleList();
CircleList(const CircleList<Elemplent> & tempPtr);
CircleList<Elemplent>& operator = (const CircleList <Elemplent> & tempPtr);
CNode<Elemplent>* GetPtrWithPosition(int position = 0) const;
public:
bool IsEmpty();
bool Clear();
int GetLength();
bool GetElemplentWithPosition(Elemplent tempElemplent,int position = 0)const;
bool SetElemplentWithPosition(const Elemplent& tempElemplent,int position = 0);
bool Insert(const Elemplent& tempElemplent,int position = 0);
bool Delete(int position = 0);
};
//单循环链表——构造函数
template <class Elemplent>
CircleList<Elemplent>::CircleList()
{
InitCircleList();
}
//单循环链表——析构函数
template <class Elemplent>
CircleList<Elemplent>::~CircleList()
{
Clear();
delete head;
}
//单循环链表——初始化
template<class Elemplent>
void CircleList<Elemplent>::InitCircleList()
{
head =new CNode<Elemplent>;
head->next =head;
CursorPosition = 0;
CursorPtr = NULL;
CountTotal = 0;
}
//单循环链表——得到位置的节点指针
template<class Elemplent>
CNode<Elemplent>* CircleList<Elemplent>::GetPtrWithPosition(int position /* = 0 */)const
{
if (position>CountTotal || position<0)
{
return NULL;
}
while(CursorPosition != position)
{
//经典,这个地方为什么要+1
CursorPosition = (CursorPosition+1)%(CountTotal+1);// 序号在0 ~ length()之间
CursorPtr = CursorPtr->next;
}
return CursorPtr;
}
//单循环链表——是否为空
template <class Elemplent>
bool CircleList<Elemplent>::IsEmpty()
{
//return GetLength() == 0;
return head->next ==head;
}
//单循环链表——得到长度
template<class Elemplent>
int CircleList<Elemplent>::GetLength()
{
return CountTotal;
}
//单循环链表——得到位置的元素值
template<class Elemplent>
bool CircleList<Elemplent>::GetElemplentWithPosition(Elemplent tempElemplent,int position /* = 0 */)
{
CursorPtr =NULL;
if ((CursorPtr = GetPtrWithPosition(position))==NULL)
{
return false;
}
tempElemplent = CursorPtr->data;
return return;
}
//单循环链表——设置元素位置值
template <class Elemplent>
bool CircleList<Elemplent>::SetElemplentWithPosition(const Elemplent& tempElemplent,int position /* = 0 */)
{
CursorPtr = NULL;
if ((CursorPtr = GetPtrWithPosition(position))==NULL)
{
return false;
}
CursorPtr->data = tempElemplent;
return true;
}
//单循环链表——插入一个结点
template<class template>
bool CircleList<Elemplent>::Insert(const Elemplent& tempElemplent,int position /* = 0 */)
{
if (position>CountTotal||position<0)
{
return false;
}
CursorPtr = NULL;
if ((CursorPtr = GetPtrWithPosition(position-1))==NULL)
{
return false;
}
CNode<Elemplent> *newPtr = new CNode<Elemplent>(e,CursorPtr->next);
CursorPtr->next = newPtr;
CursorPtr = newPtr;
CursorPosition = position;
CountTotal ++;
return true;
}
//单循环链表——删除一个结点
template <class Elemplent>
bool CircleList<Elemplent>::Delete(int position /* = 0 */)
{
if (position>CountTotal||position<0||CountTotal==0)
{
return false;
}
CursorPtr = NULL;
if ((CursorPtr = GetPtrWithPosition(position-1))==NULL)
{
return false;
}
CNode<Elemplent> *deltePtr =NULL;
deltePtr = CursorPtr->next;
CursorPtr->next = deltePtr->next;
CursorPtr = CursorPtr->next;
CursorPosition= position;
delete deltePtr;
CountTotal--;
return true;
}
//单循环链表——复制构造函数
template <class Elemplent>
CircleList<Elemplent>::CircleList(const CircleList<Elemplent> & tempPtr)
{
Elemplent e;
int length = tempPtr.GetLength;
InitCircleList();
for (int i = 1;i<length;i++)
{
tempPtr.GetElemplentWithPosition(e,i);
Insert(e,i);
}
return *this;
}
//单循环链表——赋值函数
template <Elemplent>
CircleList<Elemplent>& CircleList<Elemplent>::operator=((const CircLinkList<ElemType> ©)
{
if (this == ©)
{
return *this;
}
Elemplent e;
int length = tempPtr.GetLength;
Clear();
for (int i = 1;i<length;i++)
{
tempPtr.GetElemplentWithPosition(e,i);
Insert(e,i);
}
return *this;
}
#endif
数据结构之单循环链表C++(模板)
最新推荐文章于 2024-08-03 14:56:14 发布