双向链表

双向链表是为了解决在链表中访问直接前驱和直接后继的问题。很简单:

抽象类定义:

//双向链表结点结构体
struct DouLinkNode
{
	int data;
	DouLinkNode *lLink,*rLink;
};
//双向链表抽象数据类型定义
class DouLinkList
{
public:
	//构造函数和析构函数
	DouLinkList()
	{
		head=NULL;
		current=head;
	}
	~DouLinkList(){}
	//插入、删除、搜索、输出 dir=0,前驱方向 dir=1,后继方向
	bool Insert(int i,int x,int dir);	
	bool Delete(int i,int dir);
	DouLinkNode *Search(int i,int dir);
	void Output(int dir);
private:
	DouLinkNode *head;
	DouLinkNode *current;		//定位指针
};

主要算法:

/*
插入算法:
1、
*/
bool DouLinkList::Insert(int i,int x,int dir)
{

	DouLinkNode *newNode;
	//如果插入的是第一个结点
	if(head==NULL&&i==0)
	{
		head=new DouLinkNode;
		if(head==NULL)
		{
			cout<<"内存分配失败"<<endl;
			return false;
		}
		//创建一个头结点,并处理其左右链接域
		head->data=x;
		head->lLink=head->rLink=head;	//初始值
		current=head;
		return true;
	}
	else
	{
		if(dir==1)	//按照后继方向插入,先处理后继结点
		{
			newNode=new DouLinkNode;
			if(newNode==NULL)
			{
				cout<<"内存分配失败"<<endl;
				return false;
			}
			newNode->data=x;
			//1、先处理新结点的后继
			newNode->rLink=current->rLink;	//统一表达,current->rLink在下一步会被改变
			current->rLink=newNode;
			newNode->rLink->lLink=newNode;	//统一表达,转大圈
			newNode->lLink=current;

			//顺序插入
			current=current->rLink;

		}
		else		//先处理前驱结点,转大圈
		{
			newNode=new DouLinkNode;
			if(newNode==NULL)
			{
				cout<<"内存分配失败"<<endl;
				return false;
			}

			newNode->data=x;

			newNode->lLink=current->lLink;
			current->lLink=newNode;
			newNode->lLink->rLink=newNode;
			newNode->rLink=current;

			//顺序插入,相当于定位
			current=current->lLink;
		}
	
	}
	return true;
}

/*
输出算法:
1、
*/
void DouLinkList::Output(int dir)
{
	DouLinkNode *output=head;
	if(dir==1)
	{
		while(output->rLink!=head)
		{
			cout<<"-------------------------------"<<endl;
			cout<<"该结点值为:"<<output->data<<endl;
			cout<<"前驱结点为:"<<output->lLink->data<<"  ";
			cout<<"后继结点为:"<<output->rLink->data<<endl;
			cout<<"-------------------------------"<<endl;
			output=output->rLink;
		}
			cout<<"-------------------------------"<<endl;
			cout<<"该结点值为:"<<output->data<<endl;
			cout<<"前驱结点为:"<<output->lLink->data<<"  ";
			cout<<"后继结点为:"<<output->rLink->data<<endl;
			cout<<"-------------------------------"<<endl;
	}
	else
	{
		while(output->lLink!=head)
		{
			cout<<"-------------------------------"<<endl;
			cout<<"该结点值为:"<<output->data<<endl;
			cout<<"前驱结点为:"<<output->lLink->data<<"  ";
			cout<<"后继结点为:"<<output->rLink->data<<endl;
			cout<<"-------------------------------"<<endl;
			output=output->lLink;
		}
			cout<<"-------------------------------"<<endl;
			cout<<"该结点值为:"<<output->data<<endl;
			cout<<"前驱结点为:"<<output->lLink->data<<"  ";
			cout<<"后继结点为:"<<output->rLink->data<<endl;
			cout<<"-------------------------------"<<endl;
	}
}

/*
删除算法:
1、
*/
bool DouLinkList::Delete(int i,int dir)
{
	int count=0;
	DouLinkNode *p;
	DouLinkNode *current=head;
	while(count!=i-1)
	{
		current=current->rLink;
		count++;
	}
	p=current->rLink;
	current->rLink=p->rLink;
	p->rLink->lLink=current->lLink;
	delete p;
	return true;
}

/*
搜索算法:
1、
*/
DouLinkNode *DouLinkList::Search(int i,int dir)
{
	int count=0;
	
	DouLinkNode *current=head;
	while(count!=i)
	{
		current=current->rLink;
		count++;
	}
	return current;

}

主程序:

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"------双向链表-------"<<endl;
	DouLinkList dll;
	dll.Insert(0,0,0);
	dll.Insert(1,1,0);
	dll.Insert(2,2,0);
	dll.Insert(3,3,0);
	dll.Insert(4,4,0);
	dll.Insert(5,5,0);
	dll.Output(1);
	
	cout<<"搜索到的结点是"<<dll.Search(3,1)->data<<endl;

	dll.Delete(3,1);
	dll.Output(1);
	return 0;
}

测试结果:

------双向链表-------
-------------------------------
该结点值为:0
前驱结点为:1 后继结点为:5
-------------------------------
-------------------------------
该结点值为:5
前驱结点为:0 后继结点为:4
-------------------------------
-------------------------------
该结点值为:4
前驱结点为:5 后继结点为:3
-------------------------------
-------------------------------
该结点值为:3
前驱结点为:4 后继结点为:2
-------------------------------
-------------------------------
该结点值为:2
前驱结点为:3 后继结点为:1
-------------------------------
-------------------------------
该结点值为:1
前驱结点为:2 后继结点为:0
-------------------------------
搜索到的结点是3
-------------------------------
该结点值为:0
前驱结点为:1 后继结点为:5
-------------------------------
-------------------------------
该结点值为:5
前驱结点为:0 后继结点为:4
-------------------------------
-------------------------------
该结点值为:4
前驱结点为:5 后继结点为:2
-------------------------------
-------------------------------
该结点值为:2
前驱结点为:5 后继结点为:1
-------------------------------
-------------------------------
该结点值为:1
前驱结点为:2 后继结点为:0
-------------------------------
请按任意键继续. . .

posted @ 2013-05-25 11:18  李VS超  阅读(316)  评论(0编辑  收藏  举报