You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复单链表求并集交集代码中的标识符、类型不匹配错误?

单链表并集交集实现的编译错误修复方案

核心错误修复步骤

1. 解决关键字冲突问题

union是C语言的保留关键字,不能用作变量名。原代码中所有使用union作为变量名的地方,替换为合法标识符(比如union_list):

// 原错误代码
node *union = NULL;
union = getUnion(List1,List2);
printList(union);

// 修复后
node *union_list = NULL;
union_list = getUnion(List1,List2);
printList(union_list);

2. 修正参数类型不匹配问题

原代码误用链表头指针head1/head2接收输入的整数,导致类型不匹配。新增临时int变量存储输入值:
在main函数变量声明区添加int val;,然后修改输入循环:

// 第一个链表输入
for(i = 0;i < n;i++){
    scanf("%d",&val);
    insertAtBeg(&List1,val);
}

// 第二个链表输入
for(i = 0;i < m;i++){
    scanf("%d",&val);
    insertAtBeg(&List2,val);
}

同时可以删除main中冗余的node *head1 = NULL;和node *head2 = NULL;,避免混淆。

3. 修复并集函数的逻辑错误

getUnion函数中if语句末尾多了分号,导致去重逻辑失效,无论条件是否成立都会执行插入操作。去掉分号即可:

// 原错误代码
if(!isPresent(result, t2->data));
    insertAtBeg(&result,t2->data);

// 修复后
if(!isPresent(result, t2->data))
    insertAtBeg(&result,t2->data);

修正后的完整代码

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

typedef struct node{
    int data;
    struct node *next;
} node;

void insertAtBeg(node **head, int ele){
    node *newnode = (node*)malloc(sizeof(node));
    newnode->data = ele;
    newnode->next = (*head);
    (*head) = newnode;
}

int isPresent(node *temp, int ele){
    node *t = temp;
    while(t != NULL){
        if(t->data == ele)
            return 1;
        t = t->next;
    }
    return 0;
}

void printList(node *n){
    while(n != NULL){
        printf("%d->",n->data);
        n = n->next;
    }
    printf("NULL\n"); // 新增NULL标识链表结尾,输出更直观
}

node* getUnion(node *head1,node *head2){
    node *result = NULL;
    node *t1 = head1;
    node *t2 = head2;
    while(t1 != NULL){
        insertAtBeg(&result, t1->data);
        t1 = t1->next;
    }

    while(t2!=NULL){
        if(!isPresent(result, t2->data))
            insertAtBeg(&result,t2->data);
        t2 = t2->next;
    }
    return result;
}

node *getIntersection(node *head1,node *head2){
    node* result = NULL;
    node* t1 = head1;
    while(t1 != NULL){
        if(isPresent(head2, t1->data))
            insertAtBeg(&result,t1->data);
        t1 = t1->next;
    }
    return result;
}

int main(){
    node *intersection = NULL;
    node *union_list = NULL;
    node *List1 = NULL;
    node *List2 = NULL;
    int i,n,m,val;
    
    printf("Enter the size of the first linked list:\n");
    scanf("%d",&n);
    printf("Enter %d elements\n",n);
    for(i = 0;i < n;i++){
        scanf("%d",&val);
        insertAtBeg(&List1,val);
    }
    printf("Displaying list 1:\n");
    printList(List1);

    printf("Enter the size of the second linked list:\n");
    scanf("%d",&m);
    printf("Enter %d elements\n",m);
    for(i = 0;i < m;i++){
        scanf("%d",&val);
        insertAtBeg(&List2,val);
    }
    printf("Displaying list 2:\n");
    printList(List2);

    union_list = getUnion(List1,List2);
    intersection = getIntersection(List1,List2);

    printf("Linked List with Union of List1 and List2:\n");
    printList(union_list);
    printf("Linked List with Intersection of List1 and List2:\n");
    printList(intersection);
    return 0;
}

内容的提问来源于stack exchange,提问作者dev0419

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:31:09