problem-whether two headless linked lists cross

来源:互联网 发布:知识产权法院知乎 编辑:程序博客网 时间:2024/04/29 03:17

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-19for(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 cyclep1 -> 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: mainint main(){node * list1 = NULL ,*list2 = NULL,*p1,*p2;p1 = list1 = new node();p2 = list2 = new node();// first action: make list one, 0-9,15-19for(int i=1;i<10;i++){p2->next = new node();p2 = p2->next;p2->data = i;}// second action: make list two,0,11-19for(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 cyclep1 -> 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