对称差.c

来自「求两个集合的对称差」· C语言 代码 · 共 139 行

C
139
字号
#include<stdio.h> 
#include<stdlib.h> 

typedef struct pointer{ 
char dat; 
struct pointer *link; 
} pointer; 


void readdata(pointer *head);//读集合 
void disp(pointer *head);   //显示集合数据 
void bing(pointer *head1,pointer *head2, pointer *head3);  //计算集合1与集合2的并 
void jiao(pointer *head1,pointer *head2, pointer *head3);   //计算集合1与集合2的交 
void cha(pointer *head1,pointer *head2, pointer *head3);    //计算集合1与集合2的差 


main(){ 

pointer *head1,*head2,*head3,*headjiao,*headbing; 
head1=(pointer *)malloc(sizeof(struct pointer)); 
head1 -> link = NULL;
head2=(pointer *)malloc(sizeof(struct pointer)); 
head2->link=NULL; 
head3=(pointer *)malloc(sizeof(struct pointer)); 
head3->link=NULL; 
headjiao = (pointer *)malloc(sizeof(struct pointer));
headjiao -> link = NULL;
headbing =  (pointer *)malloc(sizeof(struct pointer));
headbing -> link = NULL;
printf("输入集合1:\n"); 
readdata(head1); 
printf("输入集合2:\n"); 
readdata(head2); 
printf("集合1为:\n"); 
disp(head1); 
printf("集合2为:\n"); 
disp(head2); 
bing(head1,head2,headbing); 
jiao(head1,head2,headjiao); 
printf("集合1与集合2的对称差为:\n"); 
cha(headbing,headjiao,head3); 
disp(head3); 

} 


void readdata(pointer *head){ //读集合 
pointer *p; 
char tmp; 
printf("input data ('0' for end):"); 
scanf("%c",&tmp); 
while(tmp!='0') 
{ 
p=(pointer *)malloc(sizeof(struct pointer)); 
p->dat=tmp; 
p->link=head->link; 
head->link=p; 
scanf("%c",&tmp); 
} 
} 

void disp(pointer *head){ //显示集合数据 
pointer *p; 
p=head->link; 
while(p!=NULL) 
{ 
printf("%c ",p->dat); 
p=p->link; 
} 
printf("\n"); 
} 

void bing(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的并 
pointer *p1,*p2,*p3; 
p1=head1->link; 
while(p1!=NULL) 
{ 
p3=(pointer *)malloc(sizeof(struct pointer)); 
p3->dat=p1->dat; 
p3->link=head3->link; 
head3->link=p3; 
p1=p1->link; 
} 
p2=head2->link; 
while(p2!=NULL) 
{ 
p1=head1->link; 
while((p1!=NULL)&&(p1->dat!=p2->dat)) 
p1=p1->link; 
if(p1==NULL) 
{ 
p3=(pointer *)malloc(sizeof(struct pointer)); 
p3->dat=p2->dat; 
p3->link=head3->link; 
head3->link=p3; 
} 
p2=p2->link; 
} 
} 

void jiao(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的交 
pointer *p1,*p2,*p3; 
p1=head1->link; 
while(p1!=NULL) 
{ 
p2=head2->link; 
while((p2!=NULL)&&(p2->dat!=p1->dat)) 
p2=p2->link; 
if((p2!=NULL)&&(p2->dat=p1->dat)) 
{ 
p3=(pointer *)malloc(sizeof(struct pointer)); 
p3->dat=p1->dat; 
p3->link=head3->link; 
head3->link=p3; 
} 
p1=p1->link; 
} 
} 

void cha(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的差 
pointer *p1,*p2,*p3; 
p1=head1->link; 
while(p1!=NULL) 
{ 
p2=head2->link; 
while((p2!=NULL)&&(p2->dat!=p1->dat)) 
p2=p2->link; 
if(p2==NULL) 
{ 
p3=(pointer *)malloc(sizeof(struct pointer)); 
p3->dat=p1->dat; 
p3->link=head3->link; 
head3->link=p3; 
} 
p1=p1->link; 
} 
} 

⌨️ 快捷键说明

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