两支针联动
//head1、head2指向非空单向链表,判断是否出现交叉点
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}ElemSN;
ElemSN *creatalink(int a[],int n)//逆向建链
{
ElemSN *p,*h=NULL;
for(int i=n-1;i>-1;i--)
{
p=(ElemSN *)malloc(sizeof(ElemSN));
p->data=a[i];
p->next=h;
h=p;
}
return h;
}
ElemSN * across(ElemSN *head1,ElemSN *head2)
{
ElemSN *p1,*p2;
p2=head2;
while(p2)
{
for(p1=head1;p1&&p1-p2;p1=p1->next)
if(!p1)
p2=p2->next;
else
break;
}
return p2;
}
int main(void)
{
int a[6]={3,1,5,6,7,2};
int b[8]={1,5,3,6,7,2,8,9};
ElemSN *head1,*head2,*PI;
head1=creatalink(a,6);
head2=creatalink(b,8);
PI=across(head1,head2);
printf("%d\n",PI->data);
}
判断链表是否出现交叉
最新推荐文章于 2022-08-01 11:27:16 发布