写一个用链表和c语言写的计算集合A和集合B的交集、并集、差集以及集合A对全集的补集和集合B对全集的补集补集的代码,要求手动输入集合A、集合B以及全集
时间: 2024-03-20 16:43:56 浏览: 134
数据结构课程设计—集合运算
好的,下面是C语言实现计算集合交集、并集、差集以及补集的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct node {
int data;
struct node* next;
} Node, *LinkedList;
LinkedList createSet();
void printSet(LinkedList set);
LinkedList unionSet(LinkedList setA, LinkedList setB);
LinkedList intersectSet(LinkedList setA, LinkedList setB);
LinkedList subtractSet(LinkedList setA, LinkedList setB);
LinkedList complementSet(LinkedList set, LinkedList universalSet);
int main() {
LinkedList setA, setB, universalSet;
printf("请输入全集,以-1结束:\n");
universalSet = createSet();
printf("请输入集合A,以-1结束:\n");
setA = createSet();
printf("请输入集合B,以-1结束:\n");
setB = createSet();
printf("集合A:");
printSet(setA);
printf("集合B:");
printSet(setB);
printf("全集:");
printSet(universalSet);
printf("集合A与集合B的交集:");
printSet(intersectSet(setA, setB));
printf("集合A与集合B的并集:");
printSet(unionSet(setA, setB));
printf("集合A与集合B的差集:");
printSet(subtractSet(setA, setB));
printf("集合A对全集的补集:");
printSet(complementSet(setA, universalSet));
printf("集合B对全集的补集:");
printSet(complementSet(setB, universalSet));
return 0;
}
LinkedList createSet() {
LinkedList head, p, q;
int data;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
p = head;
while (1) {
scanf("%d", &data);
if (data == -1) {
break;
}
q = (Node*)malloc(sizeof(Node));
q->data = data;
q->next = NULL;
p->next = q;
p = q;
}
return head;
}
void printSet(LinkedList set) {
LinkedList p = set->next;
printf("{");
while (p) {
printf("%d", p->data);
if (p->next) {
printf(", ");
}
p = p->next;
}
printf("}\n");
}
LinkedList unionSet(LinkedList setA, LinkedList setB) {
LinkedList head, p, q;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
p = head;
setA = setA->next;
setB = setB->next;
while (setA && setB) {
q = (Node*)malloc(sizeof(Node));
if (setA->data < setB->data) {
q->data = setA->data;
setA = setA->next;
} else if (setA->data > setB->data) {
q->data = setB->data;
setB = setB->next;
} else {
q->data = setA->data;
setA = setA->next;
setB = setB->next;
}
q->next = NULL;
p->next = q;
p = q;
}
if (setA) {
p->next = setA;
}
if (setB) {
p->next = setB;
}
return head;
}
LinkedList intersectSet(LinkedList setA, LinkedList setB) {
LinkedList head, p, q;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
p = head;
setA = setA->next;
setB = setB->next;
while (setA && setB) {
if (setA->data < setB->data) {
setA = setA->next;
} else if (setA->data > setB->data) {
setB = setB->next;
} else {
q = (Node*)malloc(sizeof(Node));
q->data = setA->data;
q->next = NULL;
p->next = q;
p = q;
setA = setA->next;
setB = setB->next;
}
}
return head;
}
LinkedList subtractSet(LinkedList setA, LinkedList setB) {
LinkedList head, p, q;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
p = head;
setA = setA->next;
setB = setB->next;
while (setA && setB) {
if (setA->data < setB->data) {
q = (Node*)malloc(sizeof(Node));
q->data = setA->data;
q->next = NULL;
p->next = q;
p = q;
setA = setA->next;
} else if (setA->data > setB->data) {
setB = setB->next;
} else {
setA = setA->next;
setB = setB->next;
}
}
if (setA) {
p->next = setA;
}
return head;
}
LinkedList complementSet(LinkedList set, LinkedList universalSet) {
LinkedList head, p, q;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
p = head;
set = set->next;
universalSet = universalSet->next;
while (set && universalSet) {
if (set->data < universalSet->data) {
q = (Node*)malloc(sizeof(Node));
q->data = universalSet->data;
q->next = NULL;
p->next = q;
p = q;
universalSet = universalSet->next;
} else if (set->data > universalSet->data) {
universalSet = universalSet->next;
} else {
set = set->next;
universalSet = universalSet->next;
}
}
while (universalSet) {
q = (Node*)malloc(sizeof(Node));
q->data = universalSet->data;
q->next = NULL;
p->next = q;
p = q;
universalSet = universalSet->next;
}
return head;
}
```
这份代码中,我们使用链表来存储集合,每个节点代表集合中的元素,节点中的`data`字段为元素值,`next`字段为指向下一个节点的指针。`createSet`函数用于创建集合,从标准输入中读入元素,以-1作为结束标志。`printSet`函数用于打印集合。`unionSet`函数用于计算两个集合的并集,`intersectSet`函数用于计算两个集合的交集,`subtractSet`函数用于计算两个集合的差集。`complementSet`函数用于计算一个集合对全集的补集,输入参数为集合和全集。这些函数的具体实现可以参考上面的代码。
阅读全文