C++实现数据结构三 双向循环链表

ListNode.h

template<typename Type> class DoubleList;

template<typename Type> class ListNode
{
private:
	friend class DoubleList<Type>;
	ListNode(): prior(NULL), next(NULL) {}
	ListNode(Type item, ListNode<Type> *p=NULL, ListNode<Type> *n = NULL):
	data(item), prior(p), next(n){}
	~ListNode()
	{
		prior = NULL;
		next = NULL;
	}
public:
	Type GetData();
	friend ostream& operator<< <Type>(ostream& ,ListNode<Type>&	);
private:
	Type data;
	ListNode<Type> *prior;
	ListNode<Type> *next;
};

template<typename Type> ostream& operator<<(ostream& os,ListNode<Type>& out){
	os<<out.data;
	return os;
}


template<typename Type>
Type ListNode<Type>::GetData()
{
	return this->data;
}


 

DoubleList.h

#include "ListNode.h"

template<typename Type> class DoubleList
{
public:
	DoubleList():head( new ListNode<Type>() )
	{
		head->prior = head;
		head->next = head;
	}
	~DoubleList()
	{
		MakeEmpty();
		delete head;
	}

public:
	void MakeEmpty();
	int Length();
	ListNode<Type> *Find( int n=0);
	ListNode<Type> *FindData(Type item);
	bool Insert(Type item, int n=0);
	Type Remove(int n=0);
	Type Get(int n=0);
	void Print();
private:
	ListNode<Type> *head;
};

template<typename Type>
void DoubleList<Type>::MakeEmpty()
{
	ListNode<Type> *pmov = head->next;
	ListNode<Type> *pdel;
	while(pmov != head )
	{
		pdel = pmov;
		pmov =pmov->next;
		delete pdel;
	}

	head->next = head;
	head->prior = head;
}

template<typename Type>
int DoubleList<Type>::Length()
{
	int count=0;
	ListNode<Type> *pr = head->next;
	ListNode<Type> *pl = head->prior;

	while(1)
	{
		if(pl->next == pr)
			break;
		if( pl == pr && pr != head)
		{
			count++;
			break;
		}

		count+=2;
		pr = pr->next;
		pl = pl->prior;
	}
	return count;
}

template<typename Type>
ListNode<Type>* DoubleList<Type>::Find(int n=0)
{
	if(n<1)
	{
		cout<<"the n is illegal"<<endl;
		return NULL;
	}

	ListNode<Type> *pmov =head;
	for(int i=0; i<n;i++)
	{
		pmov = pmov->next;
		if(pmov == head)
		{
			cout<<"the n is out of the boundary"<<endl;
			return NULL;
		}
	}
	return pmov;
}

template<typename Type>
ListNode<Type>* DoubleList<Type>::FindData(Type item)
{
	ListNode<Type> *pright = head->next;
	ListNode<Type> *pleft = head->prior;
	while(1)
	{
		if(pleft->next == pright)
			return NULL;
		if(pleft == pright && pright != head)
		{
			if( pright->data == item )
				return pright;
			else 
				return NULL;
		}

		if(pright->data == item )
			return pright;
		if(pleft->data == item )
			return pleft;

		pright= pright->next;
		pleft = pleft->prior;
	}
}

template<typename Type>
bool DoubleList<Type>::Insert( Type item, int n=0)
{
	if(n<0)
	{
		cout<<"the n is illegal"<<endl;
		return false;
	}
	if(n == 0)
	{
		ListNode<Type> *pinsert = new ListNode<Type>(item);
		if(head->next == head)
		{
			pinsert->next =head;
			pinsert->prior = head;
			head->next = pinsert;
			head->prior = pinsert;
			
		}
		else
		{
			pinsert->next =head->next;
			head->next->prior =pinsert;
			head->next = pinsert;
			pinsert->prior = head;
		}
		return true;
	}

	ListNode<Type> *p =Find(n);
	if(p == NULL)
	{
		cout<<"can not insert the data"<<endl;
		return false;
	}

	ListNode<Type> *pinsert = new ListNode<Type>(item);
	pinsert->next = p->next;
	p->next->prior = pinsert;
	p->next = pinsert;
	pinsert->prior= p;
	return true;
}

template<typename Type>
Type DoubleList<Type>::Remove(int n=0)
{
	if(n<1)
	{
		cout<<"the n is illegal"<<endl;
		return 0;
	}

	ListNode<Type> *pdel =Find(n);
	if(pdel == NULL)
	{
		cout<<"cannot remove the node at "<<n<<endl;
		return 0;
	}
	Type item;
	if( head->next->next == head)
	{
		head->next=head;
		head->prior=head;
		item = pdel->data;
		delete pdel;
		return item;
	}

	pdel->prior->next = pdel->next;
	pdel->next->prior =pdel->prior;
	item = pdel->data;
	delete pdel;
	return item;
}

template<typename Type>
Type DoubleList<Type>::Get(int n=0)
{
	if(n<1)
	{
		cout<<"the n is illegal"<<endl;
		return 0;
	}
	ListNode<Type> *p = Find(n);
	if(p == NULL)
	{
		cout<<"can not get the data at the "<<n<<" node"<<endl;
		return 0;
	}

	return p->data;
}

template<typename Type>
void DoubleList<Type>::Print()
{
	ListNode<Type> *pmov =head->next;
	cout<<"head-->";
	while(pmov != head)
	{
		cout<<pmov->data<<"-->";
		pmov = pmov->next;
	}
	cout<<"over"<<endl;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值