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

#ifndef STL_LINK_DOUBLELIST_H
#define STL_LINK_DOUBLELIST_H

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



/************************************************************************/
/* 双链表--节点定义
/************************************************************************/
template <class Elemplent>
class DoubleNode
{
protected:
	Elemplent data;
	DoubleNode<Elemplent> *next;
	DoubleNode<Elemplent> *prior;
public:
	DoubleNode();
	DoubleNode(Elemplent tempData,DoubleNode<Elemplent> *tempNext,DoubleNode<Elemplent> *tempPrior);
};

//节点--构造函数
template<class Elemplent>
DoubleNode<Elemplent>::DoubleNode()
{
	next = NULL;
	prior =NULL;
}

//节点--创建一个节点构造函数
template<class Elemplent>
DoubleNode<Elemplent>::DoubleNode(Elemplent tempData,
	DoubleNode<Elemplent> *tempNext,DoubleNode<Elemplent> *tempPrior)
{
	data = tempData;
	next = tempNext;
	prior = tempPrior;
}

/************************************************************************/
/* 双链表
/************************************************************************/

template<class Elemplent>
class DoubleList
{
public:
	DoubleNode<Elemplent> *head;
	mutable DoubleNode<Elemplent> *CursorNodePtr;
	mutable int CursorNodePosition;
	int CountTotal;
public:
	DoubleList();
	DoubleList(const DoubleList<Elemplent>& tempDoublelist);
	DoubleList<Elemplent>& operator = (const DoubleList<Elemplent> &tempDoublelist);
	~DoubleList();
	void InitDoubleList();
	DoubleNode<Elemplent>* GetPTRWithPostion(int position = 0);
	
public:
	int GetLength();
	bool IsEmpty();
	bool Clear();
	bool SetElemplentWithPosition(const Elemplent &tempElemplent,int position = 0);
	bool GetElemplentWihtPosition(Elemplent & tempElemplent,int position = 0)const;
	bool Insert(const Elemplent &tempElemplent,int position = 0);
	bool Delete(int position = 0);


};

//双链表构造函数
template<class Elemplent>
DoubleList<Elemplent>::DoubleList()
{
	InitDoubleList();
}

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

//双链表初始化函数
template<class Elemplent>
void DoubleList<Elemplent>::InitDoubleList()
{
	head = new DoubleNode();
	head->next = head;
	head->prior =head;
	CursorNodePosition =0;
	CursorNodePtr =NULL;
	CountTotal = 0;
}

//双链表--返回节点位置的指针--方法有些笨,但是易看易懂
template <class Elemplent>
DoubleNode<Elemplent>* DoubleList<Elemplent>::GetPTRWithPostion(int position /* = 0 */)
{
	if (position>CursorNodePosition)
	{
		while(position>CursorNodePosition || CursorNodePtr != head)
		{
			CursorNodePosition--;
			CursorNodePtr = CursorNodePtr->prior;
		}
		if (position == CursorNodePosition)
		{
			return CursorNodePtr;
		}
		else
		{
			return NULL;
		}
	}
	else if (position<CursorNodePosition)
	{
		while(position<CursorNodePosition || CursorNodePtr !=NULL)
		{
			CursorNodePosition++;
			CursorNodePtr = CursorNodePtr->next;
		}
		if (position == CursorNodePosition)
		{
			return CursorNodePtr;
		}
		else
		{
			return NULL;
		}
	}
}

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

template<class Elemplent>
bool DoubleList<Elemplent>::IsEmpty()
{
	return head->next == head;
}

//双链表--清空表
template<class Elemplent>
bool DoubleList<Elemplent>::Clear()
{
	while(GetLength()>0)
	{
		delete(1);
	}
	return IsEmpty() == true;
}

//双链表--用位置得到该节点的元素值
template <class Elemplent>
bool DoubleList<Elemplent>::GetElemplentWihtPosition(Elemplent & tempElemplent,int position /* = 0 */)const
{
	DoubleNode<Elemplent> *tempPtr;
	if ((tempPtr=GetPTRWithPostion(position) ==NULL))
	{
		return false;
	}
	tempElemplent = tempPtr->data;
	return true;
}

//双链表--用位置设置该节点的元素值
template <class Elemplent>
bool DoubleList<Elemplent>::SetElemplentWithPosition(const Elemplent &tempElemplent,int position /* = 0 */)
{
	DoubleNode<Elemplent>* tempPtr;
	if ((tempPtr=GetPTRWithPostion(position)==NULL))
	{
		return false;
	}
	tempPtr->data = tempElemplent;
	return true;
}

//双链表--插入一个元素
template <class Elemplent>
bool DoubleList<Elemplent>::Insert(const Elemplent &tempElemplent,int position /* = 0 */)
{
	DoubleNode<Elemplent> *tempPtr;
	if ((tempPtr = GetPTRWithPostion(position-1)==NULL))
	{
		return false;
	}
	DoubleNode<Elemplent> *newPtr =new DoubleNode<Elemplent>(tempElemplent,tempPtr->next);
	newPtr->next->prior=tempPtr;
	tempPtr->next=newPtr;
	newPtr->prior =tempPtr;

	CursorNodePtr = newPtr;
	CursorNodePosition = position;

	CountTotal++;
	return true;
}

//双链表--删除一个元素
template<class Elemplent>
bool DoubleList<Elemplent>::Delete(int position)
{
	DoubleNode<Elemplent> *tempPtr;
	if ((tempPtr = GetPTRWithPostion(position-1) == NULL))
	{
		return false;
	}
	tempPtr = tempPtr->next;
	tempPtr->prior->next = tempPtr->next;
	tempPtr->next->prior = tempPtr->prior;
	if (position == CountTotal)
	{
		CursorNodePosition = 0;
		CursorNodePtr = head;
	}
	else
	{
		CursorNodePtr = tempPtr->next;
		CursorNodePosition = position ;
	}

	delete tempPtr;
	CountTotal --;
	return true;
	
}

//双链表--复制构造函数
template <class Elemplent>
DoubleList<Elemplent>::DoubleList(const DoubleList<Elemplent>& tempDoublelist)
{
	Elemplent tempElemplent;
	int length = tempDoublelist.GetLength();
	InitDoubleList();
	for (int position = 1;position <=length;position++)
	{
		tempDoublelist.GetElemplentWihtPosition(tempElemplent,position);
		Insert(tempElemplent,position);
	}
	return *this;
}

//双链表--赋值操作
template<class Elemplent>
DoubleList<Elemplent>& DoubleList<Elemplent>::operator=(const DoubleList<Elemplent>& tempDoublelist)
{
	if (this == tempDoublelist)
	{
		return *this;
	}
	Clear();
	Elemplent tempElemplent;
	int length = tempDoublelist.GetLength();
	for (int position =1;position<=length;position++)
	{
		tempDoublelist.GetElemplentWihtPosition(tempElemplent,position);
		Insert(tempElemplent,position);
	}
	return *this;
}

#endif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值