#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LinkedList{
int data;
struct LinkedList *next;
}LinkedList,*List,*List2; //直接定义两个头指针
int a[5]={1,2,3,4,5};
int b[5]={4,5,6,7,8};
int n=5;
void build(List &L,List &L2){//直接建立两个头指针指向不同的链表
L=(List)malloc(sizeof(LinkedList));
L2=(List)malloc(sizeof(LinkedList));
LinkedList *s1=L->next;
LinkedList *s2=L2->next;
LinkedList *r=L;
LinkedList *r2=L2;
for(int i=0;i<n;i++){
s1=(LinkedList *)malloc(sizeof(LinkedList));
s1->data=a[i];
r->next=s1;
r=r->next;
}
r->next=NULL;
for(int i=0;i<n;i++){
s2=(LinkedList *)malloc(sizeof(LinkedList));
s2->data=b[i];
r2->next=s2;
r2=r2->next;
}
r2->next=NULL;
}
void show(List &L){
LinkedList *s=L->next;
while(s){
printf("%d ",s->data);
s=s->next;
}
}
void find(List &L,List2 L2){
LinkedList *s1=L->next;
LinkedList *s2;
while(s1){
s2=L2->next;
while(s2){
if(s1->data==s2->data){//判断
printf("%d ",s1->data);
break;//直接跳出
}else{
s2=s2->next;
}
}
s1=s1->next;
}
}
int main(){
List L;
List2 L2;
build(L,L2);
show(L);
printf("\n");
show(L2);
printf("\n公共节点:\n");
find(L,L2);
}