用C语言的有序链表实现集合的并、交、差运算,集合中的元素限定为小写字母、
时间: 2023-05-21 19:00:39 浏览: 107
数字和下划线,每个元素长度不超过20个字符。
可以使用以下代码实现有序链表:
```
typedef struct node {
char data[21];
struct node *next;
} Node;
typedef struct {
Node *head;
int size;
} List;
void init(List *list) {
list->head = NULL;
list->size = 0;
}
void insert(List *list, char *data) {
Node *node = (Node *)malloc(sizeof(Node));
strcpy(node->data, data);
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
Node *prev = NULL;
Node *curr = list->head;
while (curr != NULL && strcmp(curr->data, data) < 0) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
node->next = list->head;
list->head = node;
} else {
node->next = prev->next;
prev->next = node;
}
}
list->size++;
}
void remove(List *list, char *data) {
Node *prev = NULL;
Node *curr = list->head;
while (curr != NULL && strcmp(curr->data, data) != 0) {
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
if (prev == NULL) {
list->head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
list->size--;
}
}
int contains(List *list, char *data) {
Node *curr = list->head;
while (curr != NULL && strcmp(curr->data, data) < 0) {
curr = curr->next;
}
return curr != NULL && strcmp(curr->data, data) == 0;
}
void print(List *list) {
Node *curr = list->head;
while (curr != NULL) {
printf("%s ", curr->data);
curr = curr->next;
}
printf("\n");
}
void union(List *list1, List *list2, List *result) {
Node *curr1 = list1->head;
Node *curr2 = list2->head;
while (curr1 != NULL && curr2 != NULL) {
if (strcmp(curr1->data, curr2->data) < 0) {
insert(result, curr1->data);
curr1 = curr1->next;
} else if (strcmp(curr1->data, curr2->data) > 0) {
insert(result, curr2->data);
curr2 = curr2->next;
} else {
insert(result, curr1->data);
curr1 = curr1->next;
curr2 = curr2->next;
}
}
while (curr1 != NULL) {
insert(result, curr1->data);
curr1 = curr1->next;
}
while (curr2 != NULL) {
insert(result, curr2->data);
curr2 = curr2->next;
}
}
void intersection(List *list1, List *list2, List *result) {
Node *curr1 = list1->head;
Node *curr2 = list2->head;
while (curr1 != NULL && curr2 != NULL) {
if (strcmp(curr1->data, curr2->data) < 0) {
curr1 = curr1->next;
} else if (strcmp(curr1->data, curr2->data) > 0) {
curr2 = curr2->next;
} else {
insert(result, curr1->data);
curr1 = curr1->next;
curr2 = curr2->next;
}
}
}
void difference(List *list1, List *list2, List *result) {
Node *curr1 = list1->head;
Node *curr2 = list2->head;
while (curr1 != NULL && curr2 != NULL) {
if (strcmp(curr1->data, curr2->data) < 0) {
insert(result, curr1->data);
curr1 = curr1->next;
} else if (strcmp(curr1->data, curr2->data) > 0) {
curr2 = curr2->next;
} else {
curr1 = curr1->next;
curr2 = curr2->next;
}
}
while (curr1 != NULL) {
insert(result, curr1->data);
curr1 = curr1->next;
}
}
int main() {
List list1, list2, result;
init(&list1);
init(&list2);
init(&result);
insert(&list1, "a");
insert(&list1, "b");
insert(&list1, "c");
insert(&list1, "d");
insert(&list1, "e");
insert(&list2, "c");
insert(&list2, "d");
insert(&list2, "e");
insert(&list2, "f");
insert(&list2, "g");
printf("List 1: ");
print(&list1);
printf("List 2: ");
print(&list2);
union(&list1, &list2, &result);
printf("Union: ");
print(&result);
init(&result);
intersection(&list1, &list2, &result);
printf("Intersection: ");
print(&result);
init(&result);
difference(&list1, &list2, &result);
printf("Difference: ");
print(&result);
return 0;
}
```
对于集合的并、交、差运算,可以分别使用 union、intersection、difference 函数实现。
阅读全文