用C语言编写一个能完成集合的交、并和差运算的程序。要求: (1)集合的元素限定为英文小写字母。 (2)用单向链表表示集合。
时间: 2024-05-06 13:18:25 浏览: 103
编制一个演示集合的并、交和差运算的程序
5星 · 资源好评率100%
下面是一个简单的 C 语言程序,可以实现集合的交、并和差运算。该程序使用单向链表表示集合,每个节点包含一个元素和一个指向下一个节点的指针。
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct node {
char data;
struct node *next;
} Node;
Node *create_set();
void print_set(Node *set);
Node *insert_node(Node *set, char data);
Node *delete_node(Node *set, char data);
Node *intersect_sets(Node *set1, Node *set2);
Node *union_sets(Node *set1, Node *set2);
Node *subtract_sets(Node *set1, Node *set2);
int main() {
Node *set1 = create_set();
Node *set2 = create_set();
printf("Please enter elements for set 1 (separated by spaces):\n");
char c;
while ((c = getchar()) != '\n') {
if (islower(c)) {
set1 = insert_node(set1, c);
}
}
printf("Please enter elements for set 2 (separated by spaces):\n");
while ((c = getchar()) != '\n') {
if (islower(c)) {
set2 = insert_node(set2, c);
}
}
printf("Set 1: ");
print_set(set1);
printf("Set 2: ");
print_set(set2);
Node *intersection = intersect_sets(set1, set2);
printf("Intersection: ");
print_set(intersection);
Node *union_set = union_sets(set1, set2);
printf("Union: ");
print_set(union_set);
Node *subtract_set = subtract_sets(set1, set2);
printf("Set 1 - Set 2: ");
print_set(subtract_set);
free(set1);
free(set2);
free(intersection);
free(union_set);
free(subtract_set);
return 0;
}
Node *create_set() {
Node *set = NULL;
return set;
}
void print_set(Node *set) {
if (set == NULL) {
printf("Empty set\n");
} else {
while (set != NULL) {
printf("%c ", set->data);
set = set->next;
}
printf("\n");
}
}
Node *insert_node(Node *set, char data) {
Node *new_node = (Node *) malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
if (set == NULL) {
set = new_node;
} else {
Node *curr = set;
while (curr->next != NULL) {
if (curr->data == data) {
free(new_node);
return set;
}
curr = curr->next;
}
if (curr->data == data) {
free(new_node);
return set;
}
curr->next = new_node;
}
return set;
}
Node *delete_node(Node *set, char data) {
if (set == NULL) {
return set;
}
Node *prev = NULL;
Node *curr = set;
while (curr != NULL) {
if (curr->data == data) {
if (prev == NULL) {
set = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
break;
}
prev = curr;
curr = curr->next;
}
return set;
}
Node *intersect_sets(Node *set1, Node *set2) {
Node *result = create_set();
while (set1 != NULL && set2 != NULL) {
if (set1->data == set2->data) {
result = insert_node(result, set1->data);
set1 = set1->next;
set2 = set2->next;
} else if (set1->data < set2->data) {
set1 = set1->next;
} else {
set2 = set2->next;
}
}
return result;
}
Node *union_sets(Node *set1, Node *set2) {
Node *result = create_set();
while (set1 != NULL && set2 != NULL) {
if (set1->data == set2->data) {
result = insert_node(result, set1->data);
set1 = set1->next;
set2 = set2->next;
} else if (set1->data < set2->data) {
result = insert_node(result, set1->data);
set1 = set1->next;
} else {
result = insert_node(result, set2->data);
set2 = set2->next;
}
}
while (set1 != NULL) {
result = insert_node(result, set1->data);
set1 = set1->next;
}
while (set2 != NULL) {
result = insert_node(result, set2->data);
set2 = set2->next;
}
return result;
}
Node *subtract_sets(Node *set1, Node *set2) {
Node *result = create_set();
while (set1 != NULL && set2 != NULL) {
if (set1->data == set2->data) {
set1 = set1->next;
set2 = set2->next;
} else if (set1->data < set2->data) {
result = insert_node(result, set1->data);
set1 = set1->next;
} else {
set2 = set2->next;
}
}
while (set1 != NULL) {
result = insert_node(result, set1->data);
set1 = set1->next;
}
return result;
}
```
该程序首先创建两个空集合,然后从标准输入中读取每个集合的元素,并插入到相应的单向链表中。然后,它计算并打印出集合的交、并和差。最后,它释放了所有动态分配的内存。
该程序的输出应该类似于下面这样:
```
Please enter elements for set 1 (separated by spaces):
a b c d e
Please enter elements for set 2 (separated by spaces):
c d e f g
Set 1: a b c d e
Set 2: c d e f g
Intersection: c d e
Union: a b c d e f g
Set 1 - Set 2: a b
```
阅读全文