基于链表的两个集合的交集

求出两个集合的交集,输出交集的元素。

输入样例:
5 5
1 3 5 7 9
1 2 3 4 5
3 4
1 2 5
2 4 5 6
0 0

输出样例:
1 3 5
2 5

#include <stdio.h>
#include <stdlib.h>

typedef struct LNode {
    int data;
    struct LNode *next;
} LNode, *LinkList;

LinkList ListInit();
void ListDestroy(LinkList *head);
void ListTailInsert(LinkList head, int x);
void ListShow(LinkList head);
void ListIntersection(LinkList A, LinkList B);

int main() {
    int m, n;

    while (scanf("%d %d%*c", &m, &n) && m && n) {
        LinkList A = ListInit();
        LinkList B = ListInit();
        int x;

        for (int i = 0; i < m; i++) {
            scanf("%d%*c", &x);
            ListTailInsert(A, x);
        }
        for (int i = 0; i < n; i++) {
            scanf("%d%*c", &x);
            ListTailInsert(B, x);
        }
        ListIntersection(A, B);
        ListShow(A);
        ListDestroy(&A);
        ListDestroy(&B);
    }
    return 0;
}

LinkList ListInit() {
    LinkList head = (LinkList) malloc(sizeof(LNode));

    head->data = 0;
    head->next = NULL;
    return head;
}

void ListDestroy(LinkList *head) {
    LNode *tmp;

    while (*head) {
        tmp = *head;
        *head = (*head)->next;
        free(tmp);
    }
}

void ListTailInsert(LinkList head, int x) {
    LNode *newNode = (LNode *) malloc(sizeof(LNode)), *tail = head;

    newNode->data = x;
    newNode->next = NULL;
    while (tail->next) tail = tail->next;
    tail->next = newNode;
}

void ListShow(LinkList head) {
    for (LNode *cursor = head->next; cursor; cursor = cursor->next) {
        printf("%d%c", cursor->data, cursor->next ? 32 : 10);
    }
}

void ListIntersection(LinkList A, LinkList B) {
    LNode *curA = A, *curB, *tmp;
    int flag;//表示A中的元素是否在B中出现

    while (curA->next) {
        flag = 0;
        for (curB = B; curB->next; curB = curB->next) {
            if (curA->next->data == curB->next->data) flag = 1;
        }
        if (!flag) {
            tmp = curA->next;
            curA->next = curA->next->next;
            free(tmp);
        } else {
            curA = curA->next;
        }
    }
}
C语言中,我们可以使用链表数据结构来实现两个集合(假设这两个集合由整数表示)的交集,并将结果存储在一个新的链表A中。这里是一个简单的步骤描述: 1. 定义链表节点结构体 `struct Node` 包含整数值 `value` 和指向下一个节点的指针 `next`。 ```c typedef struct Node { int value; struct Node* next; } Node; ``` 2. 创建一个函数 `findIntersection(Node* list1, Node* list2)` 来计算并返回两个链表交集。首先遍历第一个链表(list1),将每个元素添加到哈希表(如 `unordered_set<int>` 或者自定义的数组)中。然后从第二个链表(list2)开始,检查每个元素是否在哈希表中存在。如果存在,则将其添加到结果链表A的新节点,并继续查找下一个元素。 ```c Node* findIntersection(Node* list1, Node* list2) { unordered_set<int> set1; // 使用哈希集合保存list1的元素 Node* result = NULL; // 结果链表的头节点 Node* current1 = list1; while (current1 != NULL) { set1.insert(current1->value); current1 = current1->next; } current1 = list2; while (current1 != NULL) { if (set1.find(current1->value) != set1.end()) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->value = current1->value; newNode->next = result; result = newNode; } current1 = current1->next; } return result; } ``` 3. 最后,创建一个新链表A,并调用这个函数将交集的结果链接起来。 ```c void createIntersectionList(Node** A, Node* intersection) { *A = intersection; } int main() { Node* list1 = ...; // 初始化第一个链表 Node* list2 = ...; // 初始化第二个链表 Node* result = findIntersection(list1, list2); createIntersectionList(&result, result); // 存储结果链表 // ... return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值