⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 判断链表是否相交 若相交
💻 CPP
字号:
#include "LList.h"
#include <iostream>
using namespace std;
///////this can test two intersect links
bool intersect(LList l1,LList l2);
void print(LList l1,LList l2);
int main() {
	//constuct two links
	int n1,n2;
	Node * temp;
	LList llist1,llist2;
	int i,j,k,m;
	for(i=0;i<100;i++)
	{
		llist1.append(i);
	}
	for(j=101;j<300;j++)
	{
		llist2.append(j);
	}
	/*temp = llist1.tail;
	llist2.setTail(temp);
	for(k=301;k<400;k++)
	{
		llist1.append(k);
	}*/
	// eraser /**/,thus comes to intersect!
	n1 = llist1.setEnd();
	n2 = llist2.setEnd();
	if(intersect(llist1,llist2))
	{
		cout << "the two links intersect!"<<endl;
		if(n1>n2) {
			m = n1 - n2;	
			while(m--)
			{
				llist1.fence = llist1.fence->next;
			}
		}
		else  {
			m = n2 - n1;
			while(m--)
			{
				llist2.fence = llist2.fence->next;
			}
		}
		print(llist2,llist1);
	}
	else
		cout << "the two links are not intersect!"<<endl;
	return 0;
}

//intersect or not
bool intersect(LList l1,LList l2) {
	if(l1.tail==l2.tail)
		return true;
	else 
		return false;
}

//print the intersect links elements
void print(LList l,LList s){
	Node*temp1,*temp2;	
	for(temp1=l.fence;temp1->next!=NULL;)
	{
		for(temp2=s.fence;temp2->next!=NULL;)
		{
			if(temp1->next==temp2->next) break;
			temp2 = temp2->next;
		}
			if(temp1->next==temp2->next) break;
			temp1=temp1->next;
	}
	while(temp1->next!=NULL)
	{
		cout << temp1->next->e<<" ";
		temp1=temp1->next;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -