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

C语言链表代码出现Segmentation Error,求排查方向

C语言链表段错误排查求助

以下是我为大学作业编写的C语言链表代码,实现了插入、删除、全打印功能:

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

typedef char element[100];

typedef struct ListNode {
    element data;
    struct ListNode *link;
} ListNode;

void error(char *message)
{
    fprintf(stderr, "%s\n", message);
    exit(1);
}
ListNode* insert_first(ListNode *head, element value){
    if(strcmp(value, "head") == 0 || strcmp(value, "NULL") == 0 ){
        printf("You should not try the keyword, 'head' and 'NULL'\n");
        return head;
    }    
    ListNode *p = (ListNode *)malloc(sizeof(ListNode));
    strcpy(p->data, value);
    p->link = head;
    head = p;
    return head;
}
void print_list(ListNode *head){
    for (ListNode *p = head; p != NULL; p = p->link)
        printf("%s->", p->data);
    printf("NULL\n");
    return;
}

ListNode* search_list(ListNode *head, element x){
    ListNode *p = head;

    if(head == NULL){
        printf("Nothing here. Insert First.\n");
        exit(1);
    }
    else if(strcmp(x, "head") == 0 || strcmp(x, "NULL") == 0 ){
        printf("You can not try the keyowrd, 'head' and 'NULL'\n");
        return head;
    }
    else{
        while (p != NULL)
        {
            if (strcmp(p->data, x) == 0) {
                printf("There is  %s.\n", x);
                return head;
            }
            p = p->link;
        }
        printf("We can't find %s.\n", x);
        exit(1);

}}

ListNode* delete(ListNode *head, element x){
    if(strcmp(x, "head") == 0 || strcmp(x, "NULL") == 0 ){
        printf("You can not try the keyowrd, 'head' and 'NULL'\n");
        return head;
    }
    else if(head == NULL){
        printf("There is nothing\n");
        return head;
    }
    else{
        ListNode *p = head;
        ListNode *pre = NULL; // 修改部分:初始化指向前一个节点的指针
        element data_index;
        strcpy(data_index, p->data);
        if(strcmp(data_index, x) == 0){
            head = head->link;
            free(p);
            return head;
        }
        else{
            while (p != NULL){
                if (strcmp(data_index, x) == 0) {
                    if(pre != NULL) // 修改部分:删除的节点不是第一个节点时
                        pre->link = p->link;
                    free(p);
                    return head;
                }
                pre = p; // 修改部分:更新前一个节点
                p = p->link;
                if(p != NULL) // 修改部分:仅当p不为NULL时更新data_index
                    strcpy(data_index, p->data);
            }
            printf("We can't find %s.\n", x);
        }
    }
    return head;}
int main(void){
    ListNode *head = NULL;
    element data;

    strcpy(data, "APPLe");
    head = insert_first(head, data);
    print_list(head);

    strcpy(data, "Kiwi");
    head = insert_first(head, data);
    print_list(head);

    search_list(head, data);

    delete(head, data);
    search_list(head, data);

    strcpy(data, "APPLe");
    search_list(head, data);
}

代码执行时在第49行if (strcmp(p->data, x) == 0) {处出现Segmentation Error。已知该错误源于非法内存引用,但无法定位并解决问题,恳请告知排查方向,无需提供完整修正代码。


排查方向

  • 检查main函数中调用delete后,是否正确接收了返回的新链表头指针:如果没有将delete的返回值赋值给head,后续操作的head可能指向已被释放的内存空间。
  • 验证search_list被调用时,传入的head指针是否有效:比如delete操作释放了原head指向的节点,但后续仍使用原head调用search_list,会导致访问已释放的内存。
  • 检查delete函数内部的指针操作逻辑:确认释放节点后,链表的指针衔接是否正确,是否存在悬空指针被后续操作引用的情况。
  • 调试时可以在关键位置打印head和p指针的地址,观察在delete和search_list调用前后,指针的有效性变化。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 21:54:54