用C语言编制一个能演示执行集合的交、并和差运算的程序。 【任务要求】 1)集合元素用小写英文字母,执行各种操作应以中文对话方式执行。 2)算法要点:利用单链表表示集合;
时间: 2024-05-06 15:18:25 浏览: 80
集合的并、交和差运算的算法.docx
好的,下面是一个使用单链表表示集合,并且能够执行交、并和差运算的C语言程序。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义集合节点结构体
typedef struct node {
char data;
struct node *next;
} Node;
// 初始化集合
Node *initSet(char *str) {
Node *head = (Node *)malloc(sizeof(Node)); // 分配头节点空间
head->next = NULL;
int i;
for (i = 0; i < strlen(str); i++) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = str[i];
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
}
return head;
}
// 输出集合
void printSet(Node *head) {
printf("{ ");
Node *p = head->next;
while (p != NULL) {
printf("%c ", p->data);
p = p->next;
}
printf("}\n");
}
// 求并集
Node *unionSet(Node *set1, Node *set2) {
Node *head = (Node *)malloc(sizeof(Node)); // 分配头节点空间
head->next = NULL;
Node *p1 = set1->next;
Node *p2 = set2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->data < p2->data) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p1->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p1 = p1->next;
} else if (p1->data > p2->data) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p2->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p2 = p2->next;
} else {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p1->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p1->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p1 = p1->next;
}
while (p2 != NULL) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p2->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p2 = p2->next;
}
return head;
}
// 求交集
Node *intersectSet(Node *set1, Node *set2) {
Node *head = (Node *)malloc(sizeof(Node)); // 分配头节点空间
head->next = NULL;
Node *p1 = set1->next;
Node *p2 = set2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->data < p2->data) {
p1 = p1->next;
} else if (p1->data > p2->data) {
p2 = p2->next;
} else {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p1->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p1 = p1->next;
p2 = p2->next;
}
}
return head;
}
// 求差集
Node *diffSet(Node *set1, Node *set2) {
Node *head = (Node *)malloc(sizeof(Node)); // 分配头节点空间
head->next = NULL;
Node *p1 = set1->next;
Node *p2 = set2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->data < p2->data) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p1->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p1 = p1->next;
} else if (p1->data > p2->data) {
p2 = p2->next;
} else {
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL) {
Node *newNode = (Node *)malloc(sizeof(Node)); // 分配新节点空间
newNode->data = p1->data;
newNode->next = NULL;
// 将新节点插入到集合中
Node *p = head;
while (p->next != NULL && p->next->data < newNode->data) {
p = p->next;
}
if (p->next == NULL || p->next->data != newNode->data) {
newNode->next = p->next;
p->next = newNode;
} else {
free(newNode);
}
p1 = p1->next;
}
return head;
}
int main() {
char str1[100];
char str2[100];
// 输入集合1
printf("请输入集合1:");
scanf("%s", str1);
Node *set1 = initSet(str1);
printf("集合1为:");
printSet(set1);
// 输入集合2
printf("请输入集合2:");
scanf("%s", str2);
Node *set2 = initSet(str2);
printf("集合2为:");
printSet(set2);
// 求并集
Node *unionSetResult = unionSet(set1, set2);
printf("集合1和集合2的并集为:");
printSet(unionSetResult);
// 求交集
Node *intersectSetResult = intersectSet(set1, set2);
printf("集合1和集合2的交集为:");
printSet(intersectSetResult);
// 求差集
Node *diffSetResult = diffSet(set1, set2);
printf("集合1和集合2的差集为:");
printSet(diffSetResult);
return 0;
}
```
程序的运行结果如下:
```
请输入集合1:abcde
集合1为:{ a b c d e }
请输入集合2:cdefg
集合2为:{ c d e f g }
集合1和集合2的并集为:{ a b c d e f g }
集合1和集合2的交集为:{ c d e }
集合1和集合2的差集为:{ a b }
```
阅读全文