problem-whether two headless linked lists cross

whether two headless linked lists cross

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:whether two headless linked lists cross;

博客时间:2014-4-20;

编程语言:C++ ;

编程坏境:Windows 7 专业版 x64;

编程工具:vs2008 32位编译器;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;


my worlds

keeping healthy is so important.

problem

check two headless linked lists cross.

solution

situation one: these lists  don't have a cycle

check they whether have ten same end.

	if( _Get_end(list1) != _Get_end(list2) )
		cout<<"the two list don't have the same end"<<endl;
	else cout<<"the two list have the same end"<<endl;

situation two: these lists have cycles


i will cut off one list, then check whether another is cut off. if another is cut off, then they cross, or not.

sub-problem: whether a linked list have a cycle.

just make two legs have different speeds, if they  meet, the list have a cycle, or not.

 


my experiments

situation one:

  

situation two:

	for(int i=1;i<10;i++)
	{
		p2->next = new node();
		p2 = p2->next;
		p2->data = i;
	}

	// second action: make list two,0,11-19
	for(int i=11;i<20;i++)
	{
		p1 -> next = new node();
		p1 = p1 -> next ;
		p1 -> data = i;
		if(i == 15)
		{
			p2 -> next = p1;
			p2 = p1;
		}
	}

	// third action: make cycle
	p1 -> next = p2 ;

result: cross

my code

test.cpp

#include<iostream>
#include<string>
using namespace std;


class node
{
public:
	int data;
	node * next;
	node(){
		data = 0;
		next = NULL;
	}
};


void _Output_list(node* L);
node * _Get_end(node * L);
bool _Cycle(node *L1);
bool _Cross(node * L1,node * L2);

// function: main
int main()
{
	node * list1 = NULL ,*list2 = NULL,*p1,*p2;
	p1 = list1 = new node();
	p2 = list2 = new node();

	// first action: make list one, 0-9,15-19
	for(int i=1;i<10;i++)
	{
		p2->next = new node();
		p2 = p2->next;
		p2->data = i;
	}

	// second action: make list two,0,11-19
	for(int i=11;i<20;i++)
	{
		p1 -> next = new node();
		p1 = p1 -> next ;
		p1 -> data = i;
		if(i == 15)
		{
			p2 -> next = p1;
			p2 = p1;
		}
	}

	// third action: make cycle
	p1 -> next = p2 ;

	//cout<<"list 1:"<<endl;
	//_Output_list(list1);
	//cout<<"list 2:"<<endl;
	//_Output_list(list2);

	cout<<_Cross(list1,list2)<<endl;

	system("pause");
	return 0;
}

void _Output_list(node* L)
{
	while(L != NULL)
	{
		cout<<L->data<<"\n";
		L = L->next ;
	}
	cout<<"list output over"<<endl;
}

node * _Get_end(node * L)
{
	while(L != NULL && L->next != NULL)
	{
		L = L->next;
	}
	if(L != NULL)
		return L;
	else return NULL;
}

bool _Cycle(node *L1)
{
	node * p1,*p2;
	p1 = p2 = L1;
	while(p1 != NULL && p2 != NULL)
	{
		p1 = p1->next;
		if(p2->next != NULL)
			p2 = (p2->next)->next;
		else return false;
		if(p1 == p2)
			return true;
	}
	return false;
}


bool _Cross(node * L1,node * L2)
{
	if(L1 != NULL && L2 != NULL)
	{
		bool c1 = _Cycle(L1);
		bool c2 = _Cycle(L2);
		if(!c1 && !c2)
		{
			if( _Get_end(L1) != _Get_end(L2) )
			{
				cout<<"the two list don't have the same end"<<endl;
				return true;
			}
			else {
				cout<<"the two list have the same end"<<endl;
				return false;
			}
		}
		else if(c1 && c2)
		{
			node * p1 = L1;
			node * p2 = L1->next;
			while(p1 != p2)
			{
				p1 = p1->next;
				p2 = p2->next->next;
			}
			node * newStart1 = p1 -> next;
			p1->next = NULL;

			if(_Cycle(L2) == true )
			{
				p1->next = newStart1;
				return false;
			}
			else 
			{
				p1->next = newStart1;
				return true;
			}

		}
		else return false;
	}
	else return false;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值