用C语言编制一个能演示执行集合的交、并和差运算的程序。 【任务要求】 1)集合元素用小写英文字母,执行各种操作应以中文对话方式执行。 2)算法要点:利用单链表表示集合;每一步用中文展示
时间: 2024-05-13 15:17:34 浏览: 91
集合的并、交和差运算.zip
5星 · 资源好评率100%
好的,我可以帮您编写这个程序。首先,我们需要定义一个结构体来表示集合的元素,它应该包含一个字符类型的成员变量来存储元素的值,以及一个指向下一个元素的指针。
```c
typedef struct node {
char value;
struct node* next;
} Node;
```
接下来,我们需要定义一些函数来操作集合。首先是创建集合的函数,它将返回一个指向集合头部的指针,并初始化为空集合。
```c
Node* create_set() {
Node* head = (Node*) malloc(sizeof(Node));
head->next = NULL;
return head;
}
```
然后,我们需要实现向集合中添加元素的函数。这个函数将接受集合的头部指针和要添加的元素,并将它插入到链表的末尾。
```c
void add_element(Node* head, char value) {
Node* current = head;
while (current->next != NULL) {
if (current->next->value == value) {
printf("元素 %c 已存在于集合中\n", value);
return;
}
current = current->next;
}
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->value = value;
new_node->next = NULL;
current->next = new_node;
printf("元素 %c 已添加到集合中\n", value);
}
```
现在我们可以开始实现集合的交、并和差运算了。首先是交运算,它将接受两个集合的头部指针,并返回一个新的集合,其中包含两个集合的交集。
```c
Node* intersection(Node* set1, Node* set2) {
Node* result = create_set();
Node* current1 = set1->next;
while (current1 != NULL) {
Node* current2 = set2->next;
while (current2 != NULL) {
if (current1->value == current2->value) {
add_element(result, current1->value);
break;
}
current2 = current2->next;
}
current1 = current1->next;
}
return result;
}
```
下一个是并运算,它也将接受两个集合的头部指针,并返回一个新的集合,其中包含两个集合的并集。
```c
Node* union_set(Node* set1, Node* set2) {
Node* result = create_set();
Node* current1 = set1->next;
while (current1 != NULL) {
add_element(result, current1->value);
current1 = current1->next;
}
Node* current2 = set2->next;
while (current2 != NULL) {
add_element(result, current2->value);
current2 = current2->next;
}
return result;
}
```
最后一个是差运算,它将接受两个集合的头部指针,并返回一个新的集合,其中包含第一个集合减去第二个集合的元素。
```c
Node* difference(Node* set1, Node* set2) {
Node* result = create_set();
Node* current1 = set1->next;
while (current1 != NULL) {
Node* current2 = set2->next;
bool found = false;
while (current2 != NULL) {
if (current1->value == current2->value) {
found = true;
break;
}
current2 = current2->next;
}
if (!found) {
add_element(result, current1->value);
}
current1 = current1->next;
}
return result;
}
```
现在我们已经实现了所有的集合运算函数,接下来让我们来编写一个程序来演示它们的使用。
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node {
char value;
struct node* next;
} Node;
Node* create_set() {
Node* head = (Node*) malloc(sizeof(Node));
head->next = NULL;
return head;
}
void add_element(Node* head, char value) {
Node* current = head;
while (current->next != NULL) {
if (current->next->value == value) {
printf("元素 %c 已存在于集合中\n", value);
return;
}
current = current->next;
}
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->value = value;
new_node->next = NULL;
current->next = new_node;
printf("元素 %c 已添加到集合中\n", value);
}
Node* intersection(Node* set1, Node* set2) {
Node* result = create_set();
Node* current1 = set1->next;
while (current1 != NULL) {
Node* current2 = set2->next;
while (current2 != NULL) {
if (current1->value == current2->value) {
add_element(result, current1->value);
break;
}
current2 = current2->next;
}
current1 = current1->next;
}
return result;
}
Node* union_set(Node* set1, Node* set2) {
Node* result = create_set();
Node* current1 = set1->next;
while (current1 != NULL) {
add_element(result, current1->value);
current1 = current1->next;
}
Node* current2 = set2->next;
while (current2 != NULL) {
add_element(result, current2->value);
current2 = current2->next;
}
return result;
}
Node* difference(Node* set1, Node* set2) {
Node* result = create_set();
Node* current1 = set1->next;
while (current1 != NULL) {
Node* current2 = set2->next;
bool found = false;
while (current2 != NULL) {
if (current1->value == current2->value) {
found = true;
break;
}
current2 = current2->next;
}
if (!found) {
add_element(result, current1->value);
}
current1 = current1->next;
}
return result;
}
void print_set(Node* set) {
printf("集合中的元素为:");
Node* current = set->next;
while (current != NULL) {
printf("%c ", current->value);
current = current->next;
}
printf("\n");
}
int main() {
Node* set1 = create_set();
Node* set2 = create_set();
add_element(set1, 'a');
add_element(set1, 'b');
add_element(set1, 'c');
add_element(set1, 'd');
printf("\n");
add_element(set2, 'c');
add_element(set2, 'd');
add_element(set2, 'e');
add_element(set2, 'f');
printf("\n");
Node* inter = intersection(set1, set2);
printf("集合1和集合2的交集为:\n");
print_set(inter);
printf("\n");
Node* uni = union_set(set1, set2);
printf("集合1和集合2的并集为:\n");
print_set(uni);
printf("\n");
Node* diff = difference(set1, set2);
printf("集合1减去集合2的差集为:\n");
print_set(diff);
return 0;
}
```
这个程序将创建两个集合,向它们中添加一些元素,然后演示如何使用交、并和差运算。
阅读全文