用C语言代码实现有两个集合A和集合B,要求设计生成集合C=A∩B的算法,其中集合A、B,C用链式存储结构表示(帮我完善一下我一直运行不了)

用C语言代码实现有两个集合A和集合B,要求设计生成集合C=A∩B的算法,其中集合A、B,C用链式存储结构表示(帮我完善一下我一直运行不出来,感谢各位giegie

struct SetNode {
    int val;
    struct SetNode *next;
};

// 创建一个新的集合节点
struct SetNode *newSetNode(int val) {
    struct SetNode *node = (struct SetNode*)malloc(sizeof(struct SetNode));
    node->val = val;
    node->next = NULL;
    return node;
}

// 将一个元素插入集合中
void insertSet(struct SetNode *set, int val) {
    struct SetNode *node = newSetNode(val);
    node->next = set->next;
    set->next = node;
}

// 判断一个元素是否在集合中
bool isInSet(struct SetNode *set, int val) {
    struct SetNode *node = set->next;
    while (node != NULL) {
        if (node->val == val) {
            return true;
        }
        node = node->next;
    }
    return false;
}

// 生成集合C=A∩B
struct SetNode *intersection(struct SetNode *A, struct SetNode *B) {
    struct SetNode *C = newSetNode(0);
    struct SetNode *node = A->next;
    while (node != NULL) {
        if (isInSet(B, node->val)) {
            insertSet(C, node->val);
        }
        node = node->next;
    }
    return C;
}



```)

仅供参考!谢谢!

img

img

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

typedef struct SetNode
{
    int val;
    struct SetNode *next;
} node;

node *head1 = NULL;
node *head2 = NULL;
node *head3 = NULL;

//初始化头结点
void link_init(node **head)
{
    *head = (node *)malloc(sizeof(node));
    (*head)->next = NULL;
}

// 创建一个新节点
node *makeNode(int val)
{
    node *p = (node *)malloc(sizeof(node));
    p->val = val;
    p->next = NULL;
    return p;
}

// 将结点插入到链表
void insert(node *head, node *in)
{
    node *p = head;
    in->next = head->next;
    p->next = in;
}

// 判断一个元素是否在集合中
bool isInSet(node *head, int val)
{
    node *p = head->next;
    while (p != NULL)
    {
        if (p->val == val)
        {
            return true;
        }
        p = p->next;
    }
    return false;
}

// 生成集合C=A∩B
void intersection(node *A, node *B)
{
    node *p = A->next;

    while (p != NULL)
    {
        if (isInSet(B, p->val))
        {
            insert(head3, makeNode(p->val));
        }
        p = p->next;
    }
}

//销毁链表释放空间
void destroy(node *head)
{
    node *q, *p = head;
    head = NULL;
    while (p)
    {
        q = p;
        p = p->next;
        free(q);
    }
}

int main(void)
{
    link_init(&head1);
    link_init(&head2);
    link_init(&head3);

    srand(time(NULL));
    //往链表A/B各随机输入20个数据
    int i;
    for (i = 0; i < 20; i++)
    {
        insert(head1, makeNode(rand() % 100));
        insert(head2, makeNode(rand() % 100));
    }

    node *p1 = head1->next;
    puts("A集合为:");
    while (p1)
    {
        printf("%d ", p1->val);
        p1 = p1->next;
    }

    node *p2 = head2->next;
    puts("\n\nB集合为:");
    while (p2)
    {
        printf("%d ", p2->val);
        p2 = p2->next;
    }

    intersection(head1, head2);

    node *p3 = head3->next;
    puts("\n\n交集为:");
    while (p3)
    {
        printf("%d ", p3->val);
        p3 = p3->next;
    }

    destroy(head1);
    destroy(head2);
    destroy(head3);
    return 0;
}