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

#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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值