为什么求交集和并集的函数在主函数里可以正常执行,而用子函数调用时却不能正常执行了呢?

1.写了一份用单链表保存集合以及求交并差集的代码,在主函数调用求交集和求并集的函数可以正常执行功能,但是在求差集时调用求交集和求并集的函数时发现这两个调用的函数并没有正常执行。原因未知。
2.#include
#include
typedef struct set{
int data;
struct set* next;
}set;
//初始化集合
set* Create(int i){
set p=(set *)malloc(sizeof(set));
p->next=NULL;
p->data=i;
return p;
}
//插入
void Insert(set
&p,int i,int data){
set head=p;set *q=(set)malloc(sizeof(set));
q->data=data;
if(i==1){
q->next=p;p=q;
}
else{i--;
while(--i)head=head->next;
q->next=head->next;
head->next=q;
}
}
//按值删除
void Delete(set p,int data){
set *head=p;set *q;
while(head && head->next->data!=data)head=head->next;
if(head->next->data==data){
q=head->next;
head->next=head->next->next;
free(q);
}
}
//按值查找
int CheckByData(set *p,int data){
set *head=p;
while(head){
if(head->data==data){
return 1;
exit(0);

}
else head=head->next;
}
return 0;
}
//清空集合
void Empty(set
&p){
set head=p;set *q;
while(head){
q=head;head=head->next;free(q);
}
}
//求两个集合的交集
set
Intersection(set* const A,set* const B){
set C=A; set *D=B;set *E=NULL;
//如果交集为空集,则E=NULL,否则为E指向一个新的分配空间并初始化
for(;D;D=D->next){
if(CheckByData(A,D->data)){
E=(set
)malloc(sizeof(set));
E->data=D->data;
E->next=NULL;
break;

}
}
for(;D;D=D->next){
if(CheckByData(A,D->data) && !CheckByData(E,D->data))Insert(E,1,D->data);
}
return E;
}
//求两个集合的并集
set* Union(set* const A,set* const B){
set *C=A;set *D=B;
for(;D;D=D->next){
if(!CheckByData(C,D->data))Insert(C,1,D->data);
}
return C;
}
//集合打印
void Print(set *p){
set *head=p;
for(;head;head=head->next)printf("%d\t",head->data);
}

//求两个集合的差集
set* Difference_Set(set* const A,set* const B){
set* C=Union(A,B);
set *D=Intersection(A,B);
Print(C);
Print(D);
for(;D;D=D->next)Delete(C,D->data);
return C;
}

int main(){
//初始化集合
set *A=Create(1);set *B=Create(1);set *C=Create(1);
//插入数到集合指定位置
Insert(A,1,2);Insert(A,1,3);
Insert(B,1,3);Insert(B,1,4);
Insert(C,1,4);Insert(C,1,5);
//按值删除
Delete(A,1);
Print(A);printf("\n");
Print(B);printf("\n");
Print(C);printf("\n");
//按值查找
if(CheckByData(B,1))printf("找到了!\n");
else printf("没找到!\n");
Print(B);
printf("\n");
//system("pause");
//清空集合
Empty(A);
//求两个集合的交集
printf("求B和C交集:");
A=Intersection(B,C);
Print(A);
printf("\n");
//求两个集合的并集
printf("求B和C并集:");
Empty(A);
A=Union(B,C);
Print(A);
printf("\n");
//求两个集合的差集
printf("求B和C差集:");
Empty(A);
A=Difference_Set(B,C);
Print(A);
printf("\n");
}
3.无报错。
4.无报错。
5.