C++双向循环链表(殷人昆版)

#include 
#include 
#include 
using namespace std;

template
struct DblNode
{
	T data;
	DblNode *lLink, *rLink;
	DblNode(DblNode *left = NULL, DblNode * right = NULL)
		:lLink(left)
		,rLink(right)
	{}
	DblNode(T d, DblNode *left = NULL, DblNode * right = NULL)
		:data(d)
		,lLink(left)
		,rLink(right)
	{}
};

template
class DblLink
{
public:
	DblLink()
	{
		first = new DblNode;
		first->lLink = first;
		first->rLink = first;
	}
	DblLink(const T& Val)
	{
		first = new DblNode(Val);
		first->lLink = first;
		first->rLink = first;
	}
	~DblLink()
	{
		makeEmpty();
		delete first;
	}
	void makeEmpty()
	{
		if(IsEmpty())
		{
			return;
		}
		DblNode *p = NULL;
		while(first->rLink != first)
		{
			p = first->rLink;
			first->rLink = p->rLink;
			p->rLink->lLink = first;
			delete p;
		}
	}
	int Length() const//计算双链表的长度
	{
		int count = 0;
		DblNode * p = first;
		while(p->rLink != first)
		{
			count++;
			p = p->rLink;
		}
		return count;
	}
	bool IsEmpty()//判空
	{
		if(first->rLink == first)
		{
			return true;
		}
		return false;
	}
	DblNode *getNode() const//取附加头结点地址
	{
		return first;
	}
	void setHead(DblNode *ptr)//设置附加头结点地址
	{
		first = ptr;
	}
	DblNode *Search(const T& x)
		//在链表中延后继方向寻找等于给定值x的结点
	{
		if(IsEmpty())
		{
			return NULL;
		}
		DblNode * p = first->rLink;
		while(p != first)
		{
			if(p->data == x)
			{
				return p;
			}
			p = p->rLink;
		}
		return p;
	}
	DblNode *Locate(int i, int d)
		//在链表中定位序号为i的结点,d=0按前驱方向,反之按后继方向
	{
		if(IsEmpty() || i < 0)
		{
			return NULL;
		}
		if(d != 0)
		{
			DblNode * current = first->rLink;
			int k = 0;
			while(current != first && k < i)
		    {
				current = current->rLink;
				k++;
		    }
			return current;
		}
		else
		{
			DblNode * current = first->lLink;
			int k = 0;
			while(current != first && k < i)
		    {
				current = current->lLink;
				k++;
		    }
			return current;
		}
	}
	bool Insert(int i, const T& x, int d)
		//在第i个结点之后插入一个包含有值x的新结点,d=0按前驱方向,反之按后继方向
	{
		DblNode * current = Locate(i,d);
		if(current == NULL)
		{
			return false;
		}
		DblNode * s = new DblNode(x);
		s->rLink = current->rLink;
		s->rLink->lLink = s;
		current->rLink = s;
		s->lLink = current;
		return true;
	}
	bool Remove(int i, T& x, int d)
		//删除第i个结点,x返回其值,d=0按前驱方向,反之按后继方向
	{
		if(IsEmpty())
		{
			return false;
		}
		DblNode * current = Locate(i-1,d);
		if(current == NULL&& current->rLink == first)
		{
			return false;
		}
		DblNode * del = current->rLink;
		current->rLink = del->rLink;
		del->rLink->lLink = current;
		x = del->data;
		delete del;
		return true;
	}
	void PushBack(const T& x)
	{
		DblNode * p = first;
		while(p->rLink!= first)
		{
			p = p->rLink;
		}
		DblNode *s = new DblNode(x);
		p->rLink = s;
		s->lLink = p;
		s->rLink = first;
		first->lLink = s;
	}
	void input()                  //输入
	{
		T x;
		cout << "请输入表中元素:";
		while((cin >> x) && x)
		{
			PushBack(x);
		}
	}
	void output()                 //输出
	{
		DblNode * p = first;
		while(p->rLink != first)
		{
			cout << p->rLink->data << "-->";
			p = p->rLink;
		}
		cout << "First" << endl;
	}
private:
	DblNode *first;
};

你可能感兴趣的:(数据结构,C++,C++,数据结构)