怎样用C语言实现集合的并交呢?
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/25 12:44:39
怎样用C语言实现集合的并交呢?
怎样用C语言实现集合的并交呢?
怎样用C语言实现集合的并交呢?
#include
#include
typedef struct pointer{
\x05char dat;
\x05struct pointer *link;
} pointer;
void readdata(pointer *head)
{ //读集合
\x05pointer *p;
\x05char tmp;
\x05printf("input data ('0' for end):");
\x05scanf("%c",&tmp);
\x05while(tmp!='0')
\x05{
\x05\x05if((tmp'z'))
\x05\x05{
\x05\x05\x05printf("输入错误!必须为小写字母!\n");
\x05\x05\x05return;
\x05\x05}
\x05\x05p=(pointer *)malloc(sizeof(struct pointer));
\x05\x05p->dat=tmp;
\x05\x05p->link=head->link;
\x05\x05head->link=p;
\x05\x05scanf("%c",&tmp);
\x05}
}
void disp(pointer *head)
{ //显示集合数据
\x05pointer *p;
\x05p=head->link;
\x05while(p!=NULL)
\x05{
\x05\x05printf("%c ",p->dat);
\x05\x05p=p->link;
\x05}
\x05printf("\n");
}
void bing(pointer *head1,pointer *head2,pointer *head3)
{ //计算集合1与集合2的并
\x05pointer *p1,*p2,*p3;
\x05p1=head1->link;
\x05while(p1!=NULL)
\x05{
\x05\x05p3=(pointer *)malloc(sizeof(struct pointer));
\x05\x05p3->dat=p1->dat;
\x05\x05p3->link=head3->link;
\x05\x05head3->link=p3;
\x05\x05p1=p1->link;
\x05}
\x05p2=head2->link;
\x05while(p2!=NULL)
\x05{
\x05\x05p1=head1->link;
\x05\x05while((p1!=NULL)&&(p1->dat!=p2->dat))
\x05\x05\x05p1=p1->link;
\x05\x05if(p1==NULL)
\x05\x05{
\x05\x05\x05p3=(pointer *)malloc(sizeof(struct pointer));
\x05\x05\x05p3->dat=p2->dat;
\x05\x05\x05p3->link=head3->link;
\x05\x05\x05head3->link=p3;
\x05\x05}
\x05\x05p2=p2->link;
\x05}
}
void jiao(pointer *head1,pointer *head2,pointer *head3)
{ //计算集合1与集合2的交
\x05pointer *p1,*p2,*p3;
\x05p1=head1->link;
\x05while(p1!=NULL)
\x05{
\x05\x05p2=head2->link;
\x05\x05while((p2!=NULL)&&(p2->dat!=p1->dat))
\x05\x05\x05p2=p2->link;
\x05\x05if((p2!=NULL)&&(p2->dat=p1->dat))
\x05\x05{
\x05\x05\x05p3=(pointer *)malloc(sizeof(struct pointer));
\x05\x05\x05p3->dat=p1->dat;
\x05\x05\x05p3->link=head3->link;
\x05\x05\x05head3->link=p3;
\x05\x05}
\x05\x05p1=p1->link;
\x05}
}
main()
{
\x05pointer *head1,*head2,*head3;
\x05head1=(pointer *)malloc(sizeof(struct pointer));
\x05head1->link=NULL;
\x05head2=(pointer *)malloc(sizeof(struct pointer));
\x05head2->link=NULL;
\x05head3=(pointer *)malloc(sizeof(struct pointer));
\x05head3->link=NULL;
\x05printf("输入集合1:\n");
\x05readdata(head1);
\x05getchar();
\x05printf("输入集合2:\n");
\x05readdata(head2);
\x05printf("集合1为:\n");
\x05disp(head1);
\x05printf("集合2为:\n");
\x05disp(head2);
\x05printf("集合1与集合2的并为:\n");
\x05bing(head1,head2,head3);
\x05disp(head3);
\x05head3->link=NULL;
\x05printf("集合1与集合2的交为:\n");
\x05jiao(head1,head2,head3);
\x05disp(head3);
}