a240925847 2022-12-14 00:55 采纳率: 100%
浏览 100
已结题

用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;
}



```)
  • 写回答

1条回答 默认 最新

  • 滴水不穿石 2022-12-17 01:07
    关注

    仅供参考!谢谢!

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月14日

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比