编程实现线性表LA和LB的交集和差集,并把结果存储到LC表当中。要求,在主函数中完成对LA和LB的初始构建,3个线性表都采用顺序存储结构。(C语言实现代码)
时间: 2024-09-27 10:10:17 浏览: 64
归并La和Lb得到新的线性表Lc.pdf
在C语言中,我们可以使用指针和循环来实现两个顺序存储线性表LA和LB的交集和差集操作。首先,我们需要定义一个结构体表示节点,然后创建链表结构。这里是一个简单的示例,假设我们已经有一个`Node`结构和`createList`函数用于构建顺序列表:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建线性表
Node* createList(int* arr, int size) {
Node* head = (Node*)malloc(sizeof(Node));
if (!head) {
printf("Memory allocation failed.\n");
return NULL;
}
head->data = arr[0];
head->next = NULL;
for (int i = 1; i < size; i++) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
free(head);
return NULL;
}
newNode->data = arr[i];
newNode->next = head->next;
head->next = newNode;
}
return head;
}
// 查找并添加交集
void intersect(Node** la, Node** lb, Node** lc) {
Node* laCursor = *la;
Node* lbCursor = *lb;
Node* lcHead = *lc;
while (laCursor && lbCursor) {
if (laCursor->data == lbCursor->data) {
// 如果元素相同,添加到LC
if (!lcHead) {
lcHead = (Node*)malloc(sizeof(Node));
if (!lcHead) {
printf("Memory allocation failed for LC.\n");
return;
}
lcHead->data = laCursor->data;
lcHead->next = NULL;
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
free(lcHead);
return;
}
newNode->data = laCursor->data;
newNode->next = lcHead->next;
lcHead->next = newNode;
}
laCursor = laCursor->next;
lbCursor = lbCursor->next;
} else if (laCursor->data < lbCursor->data) {
laCursor = laCursor->next;
} else {
lbCursor = lbCursor->next;
}
}
}
// 找出LA中不在LB的元素
void difference(Node* la, Node* lb, Node** lc) {
Node* laCursor = la;
Node* lcHead = *lc;
while (laCursor) {
bool found = NO;
Node* lbCursor = lb;
while (lbCursor) {
if (laCursor->data == lbCursor->data) {
found = YES;
break;
}
lbCursor = lbCursor->next;
}
if (!found) {
if (!lcHead) {
lcHead = (Node*)malloc(sizeof(Node));
if (!lcHead) {
printf("Memory allocation failed for LC.\n");
return;
}
lcHead->data = laCursor->data;
lcHead->next = NULL;
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
free(lcHead);
return;
}
newNode->data = laCursor->data;
newNode->next = lcHead->next;
lcHead->next = newNode;
}
}
laCursor = laCursor->next;
}
}
int main() {
// 假设LA和LB的数组在这里,例如 LA = {1, 2, 3, 4, 5} 和 LB = {3, 4, 6, 7}
int la[] = {1, 2, 3, 4, 5};
int lb[] = {3, 4, 6, 7};
int laSize = sizeof(la) / sizeof(la[0]);
int lbSize = sizeof(lb) / sizeof(lb[0]);
Node* laList = createList(la, laSize);
Node* lbList = createList(lb, lbSize);
Node* lcIntersect = NULL;
intersect(&laList, &lbList, &lcIntersect); // 交集
Node* lcDifferenceLaFromLb = NULL;
difference(laList, lbList, &lcDifferenceLaFromLb); // LA减去LB
// 输出LC的结果
Node* lcCursor = lcIntersect;
printf("Intersection:\n");
while (lcCursor) {
printf("%d ", lcCursor->data);
lcCursor = lcCursor->next;
}
printf("\n");
lcCursor = lcDifferenceLaFromLb;
printf("Difference (LA - LB):\n");
while (lcCursor) {
printf("%d ", lcCursor->data);
lcCursor = lcCursor->next;
}
printf("\n");
// 清理内存
cleanUp(laList);
cleanUp(lbList);
cleanUp(lcIntersect);
cleanUp(lcDifferenceLaFromLb);
return 0;
}
// 清理链表内存
void cleanUp(Node* list) {
if (list) {
Node* temp = list;
while (temp) {
Node* nextTemp = temp->next;
free(temp);
temp = nextTemp;
}
}
}
```
这个程序首先创建了LA和LB的线性表,然后分别计算它们的交集和差集,并将结果存储在LC中。注意,这个实现假设数组的大小已知,并且在处理完数据后需要手动释放内存以防止内存泄漏。
阅读全文