c语言链表集合求并集用字母表示,c语言实现的链表集合的并集与交集.doc

#includetypedef int Datatype;

//定义链表的节点

typedef struct LNode{

Datatype data;

LNode *next;

}LNode,*LinkList;

bool InitLink(LinkList &L) //初始化链表

{

L =(LinkList)malloc(sizeof(LNode));

if(L==NULL){

return false;

}

L->next=NULL;

return true;

}

bool InsertData(LinkList &L,Datatype data) //向链表中插入数据

{

LinkList pa=L;

while(pa->next!=NULL){

pa=pa->next;

}

LinkList p=(LinkList)malloc(sizeof(LNode));//新建数据节点

if(p==NULL){

printf("插入数据失败\n");

return false;

}

p->data=data;

p->next=NULL;

if(pa==NULL){

pa=p;

}else{

pa->next=p;

}

return true;

}

void createLink(LinkList &L)

{

printf("请输入要插入的数据,以0结束!\n");

Datatype data;

scanf("%d",&data);

while(data!=00){

InsertData(L,data);

scanf("%d",&data);

}

}

void printLink(LinkList L)//打印链表

{

LinkList p=L->next;

while(p!=NULL){

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

p=p->next;

}

printf("\n");

}

bool merger(LinkList La,LinkList Lb,LinkList &Lc)//两个链表求并集,并将结果存放在Lc中

{

LinkList pa,pb;

pb=Lb->next; //用于循环

pa=La->next;

while(pa!=NULL){ //以La为基础链,若B中的数据在A链中不存在,则插入到Lc中

InsertData(Lc,pa->data);

pa=pa->next;//pos表示la链中最后一个数据节点,用于插入数据

}

bool flag=false;

while(pb!=NULL){

pa=La->next;

flag=false; //用于判断B中的数据是否在A中存在

while(pa!=NULL){

if(pa->data==pb->data){

flag=false;

break; //如果存在,则本次循环结束

}

else{

flag=true;

}

pa=pa->next; //pa向后移动

}

if(flag){

/*LinkList p=(LinkList)malloc(sizeof(LNode)); //注释的代码是用于将A和B合并之后存放在A中

if(p==NULL){

printf("插入数据失败\n");

return false;

}

p->data=pb->data;

p->next=NULL;

pos->next=p;

pos=p;*/

InsertData(Lc,pb->data); //将B中的数据插入到Lc中

}

pb=pb->next;

}

return true;

}

bool Intersection(LinkList La,LinkList Lb,LinkList &Lc)//两个链表求交集,结果存放在Lc中

{

LinkList pa,pb;

pb=Lb->next;

while(pb!=NULL){

pa=La->next;

while(pa!=NULL){

if(pa->data==pb->data){ //求交集时,只需找到两个链中共同的数据,插入到Lc中即可

InsertData(Lc,pa->data);

}

pa=pa->next;

}

pb=pb->next;

}

return true;

}

int main()

{

LinkList La,Lb,Lc;

if(!InitLink(La)){

printf("初始化链表失败");

exit(1);

}

if(!InitLink(Lb)){

printf("初始化链表失败");

exit(1);

}

if(!InitLink(Lc)){

printf("初始化链表失败");

exit(1);

}

printf("创建第一条链表\n");

createLink(La);

printf("创建第二条链表\n");

createLink(Lb);

printf("链表中的数据为\n");

printf("La: ");

printLink(La);

printf("Lb: ");

printLink(Lb);

printf("链表求并集\n");

merger(La,Lb,Lc);

printf("合并之后链表中的数据为\n");

printLink(Lc);

printf("链表求交集\n");

if(!InitLink(Lc)){

printf("初始化链表失败");

exit(1);

}

Intersection(La,Lb,Lc);

printf("求交之后链表中的数据为\n");

printLink(Lc);

return(0);

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表实现集合的创建 ``` #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node* next; }Node; void add_node(Node** head,int data) { Node* new_node = (Node*)malloc(sizeof(Node)); if(new_node == NULL) { printf("Error! Memory is not allocated."); exit(0); } new_node->data = data; new_node->next = *head; *head = new_node; } void display(Node* head) { Node* current = head; while(current != NULL) { printf("%d ",current->data); current = current->next; } printf("\n"); } void set_union(Node* head1,Node* head2) { Node* current1 = head1; Node* current2 = head2; Node* result = NULL; while(current1 != NULL && current2 != NULL) { if(current1->data < current2->data) { add_node(&result,current1->data); current1 = current1->next; } else if(current1->data > current2->data) { add_node(&result,current2->data); current2 = current2->next; } else { add_node(&result,current1->data); current1 = current1->next; current2 = current2->next; } } while(current1 != NULL) { add_node(&result,current1->data); current1 = current1->next; } while(current2 != NULL) { add_node(&result,current2->data); current2 = current2->next; } display(result); } void set_intersection(Node* head1,Node* head2) { Node* current1 = head1; Node* current2 = head2; Node* result = NULL; while(current1 != NULL && current2 != NULL) { if(current1->data < current2->data) { current1 = current1->next; } else if(current1->data > current2->data) { current2 = current2->next; } else { add_node(&result,current1->data); current1 = current1->next; current2 = current2->next; } } display(result); } int main() { Node* set1 = NULL; Node* set2 = NULL; add_node(&set1,5); add_node(&set1,7); add_node(&set1,9); add_node(&set1,11); printf("Set1: "); display(set1); add_node(&set2,4); add_node(&set2,7); add_node(&set2,11); add_node(&set2,13); printf("Set2: "); display(set2); printf("Union of Set1 and Set2: "); set_union(set1,set2); printf("Intersection of Set1 and Set2: "); set_intersection(set1,set2); return 0; } ``` 输出结果: ``` Set1: 11 9 7 5 Set2: 13 11 7 4 Union of Set1 and Set2: 13 11 9 7 5 4 Intersection of Set1 and Set2: 11 7 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值