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

C语言链表程序段错误:头部/尾部插入功能异常求助

链表段错误与插入功能失效问题排查与修复

核心问题分析

你的代码出现段错误、插入功能失效的原因集中在两个关键位置:

  1. 未为插入函数的新节点分配内存
    在insertAtBeginning和insertAtEnd函数中,仅声明了struct Node *newnode指针,却没有通过malloc为其分配内存空间。直接通过野指针访问newnode->data和newnode->link,必然触发段错误,这也是你怀疑的newnode->data = info语句报错的根本原因。

  2. 尾部插入的循环逻辑错误
    insertAtEnd中的循环while(temp!=NULL)会让temp最终指向NULL,此时执行temp->link = newnode属于对空指针解引用,同样会引发段错误。正确逻辑应该是遍历到链表的最后一个有效节点(即temp->link == NULL的节点)。

修复后的完整代码

#include<stdio.h>
#include<stdlib.h>
// 移除非标准库conio.h,提升代码可移植性
void insertAtBeginning(int);
void insertAtEnd(int);
void printLL();

struct Node
{
    int data;
    struct Node *link;
};
struct Node *head;


int main()
{
    struct Node *temp, *newnode;
    int ch=1, info;
    head = NULL;
    while(ch)
    {
        printf("Enter data: ");
        scanf("%d", &info);
        
        newnode = (struct Node *)malloc(sizeof(struct Node));
        // 增加内存分配失败检查
        if(newnode == NULL){
            printf("Memory allocation failed!\n");
            return 1;
        }
        
        newnode->data = info;
        newnode->link = NULL;
        
        if(head == NULL)
        {
            head = newnode;
            temp = newnode;
        }
        else
        {
            temp->link = newnode;
            temp = newnode;
        }
        printf("You wish to continue? (press 0 to terminate)\n");
        scanf("%d",&ch);
    }
    printf("Original linked list: ");
    temp = head;
    while(temp!=NULL)
    {
        printf("%d -> ",temp->data);
        temp = temp->link;
    }
    printf("NULL\n");
    
    insertAtBeginning(50);
    insertAtEnd(150);
    
    printf("Linked list after insertions: ");
    printLL();
    printf("NULL\n");
    
    return 0;
}

void insertAtBeginning(int info)
{
    struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
    if(newnode == NULL){
        printf("Memory allocation failed!\n");
        return;
    }
    newnode->data = info;
    newnode->link = head;
    head = newnode;
    printf("Inserted %d at beginning\n", info);
}

void insertAtEnd(int info)
{
    struct Node *temp, *newnode = (struct Node *)malloc(sizeof(struct Node));
    if(newnode == NULL){
        printf("Memory allocation failed!\n");
        return;
    }
    newnode->link = NULL;
    newnode->data = info;
    
    // 处理链表为空的边界情况
    if(head == NULL){
        head = newnode;
        printf("Inserted %d at end (list was empty)\n", info);
        return;
    }
    
    temp = head;
    // 遍历到最后一个有效节点
    while(temp->link != NULL)
    {
        temp = temp->link;
    }
    temp->link = newnode;
    printf("Inserted %d at end\n", info);
}

void printLL()
{
    struct Node *temp = head;
    while(temp!=NULL)
    {
        printf("%d -> ",temp->data);
        temp = temp->link;
    }
}

额外优化说明

  • 移除非标准库conio.h,避免环境兼容性问题
  • 增加malloc空指针检查,防御内存分配失败的异常情况
  • 优化输出提示,让操作结果更直观
  • 修复main函数中冗余的break语句,简化循环逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:25:23