帮我解决一下我的求链表交集的问题
程序代码:
#include<stdio.h> #include<stdlib.h> struct number{ int data; struct number*next ; }; struct number*creat() //创建 { struct number*p1,*p2,*sq; p2=sq=(struct number*)malloc(sizeof(struct number)); p1=(struct number*)malloc(sizeof(struct number)); scanf(" %d",&p1->data); while(p1->data!=0) {p2->next=p1; p2=p1; p1=(struct number*)malloc(sizeof(struct number)); scanf(" %d",&p1->data); } return sq; } void print(struct number*sq) //输出 { struct number*p1; p1=sq->next; while(p1!=NULL) { printf(" %d",p1->data); p1=p1->next; } } void main() { struct number*l1,*l2,*l3,*pa,*pb,*t,*t1; l1=creat(); l2=creat(); pa=l1->next; pb=l2->next; l3=t1=(struct number*)malloc(sizeof(struct number)); //为l3链表建立头结点 while(pa!=NULL) { while(pb!=NULL) //比较pa和pb的data { //如果不等表2就下移,表1不动 if(pa->data!=pb->data) pb=pb->next; else { t=(struct number*)malloc(sizeof(struct number)); //如果相等接入表3 t->data=pb->data; t1->next=t; t1=t; } pa=pa->next; pb=l2->next; //当表2中的所有元素与表1的当前元素比较完了,表1再下移一位,以此类推 } } print(l1); print(l2); print(l3); }