请你设计C程序进行集合计算。要求先用链表存储两个正整数集合A和B(从键盘输入集合的元素,以-1结束输入),然后计算A和B的交集、并集与差集A-B,再输出计算结果,最后删除所有动态变量。
时间: 2024-01-22 20:20:45 浏览: 62
用于处理数据,求两个集合的交集、并集、补集。附源码。
下面是实现这个功能的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建新节点
Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
// 在链表末尾添加新节点
void appendNode(Node** head, int data) {
Node* node = createNode(data);
if (*head == NULL) {
*head = node;
} else {
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}
}
// 删除链表
void deleteList(Node** head) {
Node* current = *head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*head = NULL;
}
// 打印集合
void printSet(Node* head) {
printf("{ ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("}\n");
}
// 计算交集
Node* intersection(Node* set1, Node* set2) {
Node* result = NULL;
Node** tail = &result;
while (set1 != NULL && set2 != NULL) {
if (set1->data == set2->data) {
appendNode(tail, set1->data);
tail = &((*tail)->next);
set1 = set1->next;
set2 = set2->next;
} else if (set1->data < set2->data) {
set1 = set1->next;
} else {
set2 = set2->next;
}
}
return result;
}
// 计算并集
Node* unionSet(Node* set1, Node* set2) {
Node* result = NULL;
Node** tail = &result;
while (set1 != NULL && set2 != NULL) {
if (set1->data == set2->data) {
appendNode(tail, set1->data);
tail = &((*tail)->next);
set1 = set1->next;
set2 = set2->next;
} else if (set1->data < set2->data) {
appendNode(tail, set1->data);
tail = &((*tail)->next);
set1 = set1->next;
} else {
appendNode(tail, set2->data);
tail = &((*tail)->next);
set2 = set2->next;
}
}
while (set1 != NULL) {
appendNode(tail, set1->data);
tail = &((*tail)->next);
set1 = set1->next;
}
while (set2 != NULL) {
appendNode(tail, set2->data);
tail = &((*tail)->next);
set2 = set2->next;
}
return result;
}
// 计算差集A-B
Node* difference(Node* set1, Node* set2) {
Node* result = NULL;
Node** tail = &result;
while (set1 != NULL && set2 != NULL) {
if (set1->data == set2->data) {
set1 = set1->next;
set2 = set2->next;
} else if (set1->data < set2->data) {
appendNode(tail, set1->data);
tail = &((*tail)->next);
set1 = set1->next;
} else {
set2 = set2->next;
}
}
while (set1 != NULL) {
appendNode(tail, set1->data);
tail = &((*tail)->next);
set1 = set1->next;
}
return result;
}
int main() {
// 输入集合A
int data;
Node* setA = NULL;
printf("请输入集合A的元素(以-1结束输入):\n");
while (1) {
scanf("%d", &data);
if (data == -1) {
break;
}
appendNode(&setA, data);
}
// 输入集合B
Node* setB = NULL;
printf("请输入集合B的元素(以-1结束输入):\n");
while (1) {
scanf("%d", &data);
if (data == -1) {
break;
}
appendNode(&setB, data);
}
// 计算并输出交集、并集和差集
printf("集合A:");
printSet(setA);
printf("集合B:");
printSet(setB);
printf("交集:");
printSet(intersection(setA, setB));
printf("并集:");
printSet(unionSet(setA, setB));
printf("差集A-B:");
printSet(difference(setA, setB));
// 删除动态变量
deleteList(&setA);
deleteList(&setB);
return 0;
}
```
以上代码使用链表来存储集合A和B,并实现了计算交集、并集和差集的功能。在程序运行结束后,还会调用`deleteList()`函数删除动态分配的内存,避免内存泄漏。
阅读全文