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