【编程语言 · C语言 · 计算集合的并集、交集、差集】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
集合是一个数学概念,表示一组对象的整体,其中每个对象都是唯一的。集合可以进行交、并、等操作,用于操作集合中的元素。在C语言中,可以使用链表来实现这些集合操作。 链表是一种数据结构,由若干个节点组成,每个节点都包括一个数据元素和指向下一个节点的指针。在C语言中,可以使用结构体来定义一个节点,如下所示: ``` struct Node { int data; struct Node* next; }; ``` 其中data表示节点存储的数据元素,next表示指向下一个节点的指针。利用这个结构体,可以定义一个链表。 链表的基本操作包括插入、删除和遍历。在集合交、并、操作中,需要对两个链表进行遍历,并根据不同的操作进行元素的增删。 假设有两个链表list1和list2,分别存储两个集合。要实现交集操作,可以遍历list1,对于其中的每个元素,判断其是否也在list2中出现,如果是,则将其添加到一个新的链表中。类似的,可以实现并集操作和集操作。 具体实现过程可以参考以下代码: ``` #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // 判断链表中是否存在某个元素 int isExist(struct Node* head, int val) { struct Node* p = head; while (p != NULL) { if (p->data == val) { return 1; } p = p->next; } return 0; } // 求两个集合交集 struct Node* intersect(struct Node* list1, struct Node* list2) { struct Node* head = NULL; // 交集链表的头结点 struct Node* tail = NULL; // 交集链表的尾结点 struct Node* p = list1; while (p != NULL) { if (isExist(list2, p->data)) { // 如果list2中也存在该元素,则添加到交集链表中 struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } return head; } // 求两个集合的并集 struct Node* unionn(struct Node* list1, struct Node* list2) { struct Node* head = NULL; // 并集链表的头结点 struct Node* tail = NULL; // 并集链表的尾结点 struct Node* p = list1; while (p != NULL) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (!isExist(head, p->data)) { // 如果并集链表中不存在该元素,则添加到链表中 if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } p = list2; while (p != NULL) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (!isExist(head, p->data)) { if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } return head; } // 求两个集合集 struct Node* subtract(struct Node* list1, struct Node* list2) { struct Node* head = NULL; // 集链表的头结点 struct Node* tail = NULL; // 集链表的尾结点 struct Node* p = list1; while (p != NULL) { if (!isExist(list2, p->data)) { // 如果list2中不存在该元素,则添加到集链表中 struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } return head; } // 打印链表 void printList(struct Node* head) { printf("["); struct Node* p = head; while (p != NULL) { printf("%d", p->data); if (p->next != NULL) { printf(", "); } p = p->next; } printf("]\n"); } int main() { // 定义两个链表,list1={1,3,5,7,9},list2={2,4,6,8,10} struct Node* list1 = (struct Node*) malloc(sizeof(struct Node)); list1->data = 1; list1->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->data = 3; list1->next->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->next->data = 5; list1->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->next->next->data = 7; list1->next->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->next->next->next->data = 9; list1->next->next->next->next->next = NULL; struct Node* list2 = (struct Node*) malloc(sizeof(struct Node)); list2->data = 2; list2->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->data = 4; list2->next->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->next->data = 6; list2->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->next->next->data = 8; list2->next->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->next->next->next->data = 10; list2->next->next->next->next->next = NULL; // 求交集 struct Node* list3 = intersect(list1, list2); printf("Intersect: "); printList(list3); // 求并集 struct Node* list4 = unionn(list1, list2); printf("Union: "); printList(list4); // 求集 struct Node* list5 = subtract(list1, list2); printf("Subtract: "); printList(list5); return 0; } ``` 运行结果如下: ``` Intersect: [ ] Union: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] Subtract: [1, 3, 5, 7, 9] ``` 说明交集为空集,表示list1和list2没有共同的元素;并集包含了list1和list2的所有元素,集包含了list1中没有出现在list2中的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟程序员__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值