单链表实现集合的交并差Java_单链表表示的集合交,并,差运算,设计采用定义集合,用集合运算表达式求值的方式进行。C语言实现。...

#includetypedef

struct

LNode//

定义结构体类型指针

{

char

data;

struct

LNode*next;

}*pointer;

void

readdata(pointer

head)//

定义输入集合函数

{

pointer

p;

char

tmp;

scanf("%c",&tmp);

while(tmp!='\n')

{

p=(pointer)malloc(sizeof(struct

LNode));

p->data=tmp;

p->next=head->next;

head->next=p;

scanf("%c",&tmp);

}

}

void

pop(pointer head)//定义输出集合函数

{

pointer

p;

p=head->next;

while(p!=NULL)

{

printf("%c",p->data);

p=p->next;

}

printf("\n");

}

void

or(pointer head1,pointer head2,pointer head3)//定义集合的交集函数

{

pointer

p1,p2,p3;

p1=head1->next;

while(p1!=NULL)

{

p2=head2->next;

while((p2!=NULL)&&(p2->data!=p1->data))

p2=p2->next;

if((p2!=NULL)&&(p2->data==p1->data))

{

p3=(pointer)malloc(sizeof(structLNode));

p3->data=p1->data;

p3->next=head3->next;

head3->next=p3;

}

p1=p1->next;

}

}

void main()//主函数

{

int x;

printf("(输入数据,按回车键结束\n");

pointerhead1,head2,head3;

head1=(pointer)malloc(sizeof(structLNode));

head1->next=NULL;

head2=(pointer)malloc(sizeof(structLNode));

head2->next=NULL;

head3=(pointer)malloc(sizeof(structLNode));

head3->next=NULL;printf("请输入集合A:\n");

readdata(head1);//调用输入集合函数

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

readdata(head2);//调用输入集合函数A:

printf("1.交集2.结束\n");

do

{

printf("请选择序号\n");

scanf("%d",&x);

switch(x)

{

case1:

or(head1,head2,head3);//调用交集函数

printf("集合A为:");

pop(head1);

printf("集合B为:");

pop(head2);

printf("两集合的交集C=A∩B:");

pop(head3);

head3->next=NULL;

break;

case2:

break;

default:

gotoA;

}

}while(x!=2);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值