用c语言计算一串字母的和,数据结构 用c语言写的 集合的并、交和差运算的程序基本要求:(1)集合的元素限定为小写字母字符〔‘a’..'z'〕.(2)演示程序以用户和计算机的对话方式执行.实现提示:以有...

7d14a2b81882cfe4494b096a84150b2a.png 优质解答

以下程序由标准C实现,并经严格测试.程序通过单链表存储集合

#include

#include

typedef struct pointer{

char dat;

struct pointer *link;

} pointer;

void readdata(pointer *head){ //读集合

pointer *p;

char tmp;

printf("input data ('0' for end):");

scanf("%c",&tmp);

while(tmp!='0')

{

if((tmp'z'))

{

printf("输入错误!必须为小写字母!\n");

return;

}

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;

}

}

main(){

pointer *head1,*head2,*head3;

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;

printf("输入集合1:\n");

readdata(head1);

printf("输入集合2:\n");

readdata(head2);

printf("集合1为:\n");

disp(head1);

printf("集合2为:\n");

disp(head2);

printf("集合1与集合2的并为:\n");

bing(head1,head2,head3);

disp(head3);

head3->link=NULL;

printf("集合1与集合2的交为:\n");

jiao(head1,head2,head3);

disp(head3);

head3->link=NULL;

printf("集合1与集合2的差为:\n");

cha(head1,head2,head3);

disp(head3);

}

测试用例为(0表示集合输入结束):

fdsa0

savc0

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值