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

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值