c语言写利用有序链表表示下整数任命,输入集合A和B,实现信息的交、并和差运算。
时间: 2024-03-14 17:46:28 浏览: 78
集合的交并差
好的,这是一个比较具体的问题,我会尽力回答。下面是我的回答:
首先,我们可以定义一个有序链表结构体来表示整数集合:
```
typedef struct node {
int value;
struct node* next;
} Node;
typedef struct {
Node* head;
int size;
} LinkedList;
```
其中,每个节点包含一个整数值和一个指向下一个节点的指针,而链表则包含一个指向头节点的指针和链表大小。
接下来,我们可以编写一些基本的操作函数来对链表进行操作。这些函数包括:
- `create_node(int value)`:创建一个新节点
- `insert_node(LinkedList* list, int value)`:将一个新节点插入到有序链表中
- `delete_node(LinkedList* list, int value)`:从有序链表中删除一个节点
- `create_linkedlist()`:创建一个新的有序链表
- `destroy_linkedlist(LinkedList* list)`:销毁一个有序链表
- `print_linkedlist(LinkedList* list)`:打印一个有序链表中的元素
有了这些基本操作函数,我们就可以实现集合的交、并和差运算了。具体来说,对于集合A和B,我们可以分别创建两个有序链表,然后进行如下操作:
- 并集:遍历A和B两个有序链表,将它们的元素插入到一个新的有序链表中,去重后即可得到A和B的并集。
- 交集:遍历A和B两个有序链表,将它们共有的元素插入到一个新的有序链表中,即可得到A和B的交集。
- 差集:先将B中的元素插入到一个新的有序链表中,然后遍历A中的元素,如果在B中出现过,则从新的链表中删除对应元素,最终得到A和B的差集。
下面是一个简单的实现代码,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node* next;
} Node;
typedef struct {
Node* head;
int size;
} LinkedList;
Node* create_node(int value) {
Node* node = (Node*)malloc(sizeof(Node));
node->value = value;
node->next = NULL;
return node;
}
void insert_node(LinkedList* list, int value) {
Node* node = create_node(value);
if (list->head == NULL || value < list->head->value) {
node->next = list->head;
list->head = node;
} else {
Node* curr = list->head;
while (curr->next != NULL && curr->next->value < value) {
curr = curr->next;
}
node->next = curr->next;
curr->next = node;
}
list->size++;
}
void delete_node(LinkedList* list, int value) {
if (list->head == NULL) {
return;
}
if (list->head->value == value) {
Node* tmp = list->head;
list->head = list->head->next;
free(tmp);
list->size--;
} else {
Node* curr = list->head;
while (curr->next != NULL && curr->next->value != value) {
curr = curr->next;
}
if (curr->next != NULL) {
Node* tmp = curr->next;
curr->next = curr->next->next;
free(tmp);
list->size--;
}
}
}
LinkedList* create_linkedlist() {
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
list->head = NULL;
list->size = 0;
return list;
}
void destroy_linkedlist(LinkedList* list) {
Node* curr = list->head;
while (curr != NULL) {
Node* tmp = curr;
curr = curr->next;
free(tmp);
}
free(list);
}
void print_linkedlist(LinkedList* list) {
printf("{ ");
Node* curr = list->head;
while (curr != NULL) {
printf("%d ", curr->value);
curr = curr->next;
}
printf("}\n");
}
LinkedList* set_union(LinkedList* A, LinkedList* B) {
LinkedList* C = create_linkedlist();
Node* currA = A->head;
Node* currB = B->head;
while (currA != NULL || currB != NULL) {
if (currB == NULL || (currA != NULL && currA->value < currB->value)) {
insert_node(C, currA->value);
currA = currA->next;
} else if (currA == NULL || (currB != NULL && currB->value < currA->value)) {
insert_node(C, currB->value);
currB = currB->next;
} else {
insert_node(C, currA->value);
currA = currA->next;
currB = currB->next;
}
}
return C;
}
LinkedList* set_intersection(LinkedList* A, LinkedList* B) {
LinkedList* C = create_linkedlist();
Node* currA = A->head;
Node* currB = B->head;
while (currA != NULL && currB != NULL) {
if (currA->value < currB->value) {
currA = currA->next;
} else if (currB->value < currA->value) {
currB = currB->next;
} else {
insert_node(C, currA->value);
currA = currA->next;
currB = currB->next;
}
}
return C;
}
LinkedList* set_difference(LinkedList* A, LinkedList* B) {
LinkedList* C = create_linkedlist();
Node* currB = B->head;
while (currB != NULL) {
insert_node(C, currB->value);
currB = currB->next;
}
Node* currA = A->head;
while (currA != NULL) {
delete_node(C, currA->value);
currA = currA->next;
}
return C;
}
int main() {
LinkedList* A = create_linkedlist();
LinkedList* B = create_linkedlist();
insert_node(A, 1);
insert_node(A, 2);
insert_node(A, 3);
insert_node(A, 4);
insert_node(A, 5);
insert_node(A, 6);
insert_node(B, 4);
insert_node(B, 5);
insert_node(B, 6);
insert_node(B, 7);
insert_node(B, 8);
printf("A: ");
print_linkedlist(A);
printf("B: ");
print_linkedlist(B);
LinkedList* C = set_union(A, B);
printf("A union B: ");
print_linkedlist(C);
destroy_linkedlist(C);
C = set_intersection(A, B);
printf("A intersection B: ");
print_linkedlist(C);
destroy_linkedlist(C);
C = set_difference(A, B);
printf("A difference B: ");
print_linkedlist(C);
destroy_linkedlist(C);
destroy_linkedlist(A);
destroy_linkedlist(B);
return 0;
}
```
阅读全文