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

C语言链表尾部插入元素后无法正确显示的技术求助

C语言链表尾部插入元素失败问题排查与修复

我无法在C语言链表的尾部插入元素并正确显示结果,以下是包含Create、Display、count(节点计数)、insert功能的基础链表实现C程序:

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

typedef struct Node      // Node structure
{
    int data;
    struct Node *next;
}Node;
Node *first=NULL;

void create(int A[],int n)    // function to create a structure
{      
    first=(Node *)malloc(sizeof(Node));
    first->data=A[0];
    first->next=NULL;
    Node *temp;
    temp=(Node*)malloc(sizeof(Node*));
    first->next=temp;
    temp->next=NULL;
    for(int i=1;i<=n;i++)
    {
        temp->next=(Node *)malloc(sizeof(Node));
        temp->data=A[i];
        if(i==n)
        {temp->next=NULL;
        break;
        }
        else
        temp=temp->next;
    }
}
int count(Node *p)       // to count the number of nodes in the list
{
    int count=0;
    while(p->next!=NULL)
    {
        count=count+1;
        p=p->next;
    }
    return count;

}
void Display(Node *p)       // function to display the list
{
   
   while(p->next!=NULL)
   {
    printf("%d ",p->data);
    p=p->next;
   }
}

void insert(int index, int key)       // function to insert the elements in the list
{
    if(index<0 || index>count(first))   // to check the index 
    {
        printf("\nErr... Invalid index");
    }

    if(index==0)                  // if index is the first node then it the new node should be 
                                      // the first node
    {
        Node *temp=(Node *)malloc(sizeof(Node));
        temp->data=key;
        temp->next=first;
        first=temp;
    }
    
    if(index==count(first))                // if new node is the last node to be inserted
    {   Node *temp=first;
        int i=1;
        //while(temp->next!=NULL)
        while(i<index)
        {
            temp=temp->next;
            i++;
        }
        Node *last=(Node *)malloc(sizeof(Node));
        if(temp->next ==NULL)
        printf("\n we are the last node");
        temp->next=last;
        last->data=key;
        //last->data=key;
        last->next=NULL;
        
    }
    if(index!=0 && index !=count(first))   // if the new node is not the first or the last
    {
        Node *temp=first;
        int count=1;
        while(count<index)
        {
            temp=temp->next;
            count++;
        }
        Node *last=(Node *)malloc(sizeof(Node));
        last->data=key;
        last->next=temp->next;
        temp->next=last;
    }
}
int main()
{ 
    int A[]={1,3,5,12};
    create(A,(sizeof(A)/sizeof(A[0])));
    insert(4,10);
    Display(first);
    return(0);
}

测试结果如下:

I/p: insert(0,10)    o/p: 10 1 3 5 12 
I/p: insert(1,10)    o/p: 1 10 3 5 12 
I/p: insert(2,10)    o/p: 1 3 10 5 12 
I/p: insert(3,10)    o/p: 1 3 5 10 12 
I/p: insert(4,10)    o/p: 1 3 5 12   -----&gt; 此处遇到问题,预期输出应为1 3 5 12 10

问题根源分析

1. create函数的逻辑错误

  • 初始化时额外分配了一个无用的temp节点,且malloc(sizeof(Node*))错误,应该分配sizeof(Node),否则内存空间不足导致后续操作异常。
  • 循环条件i<=n错误:数组A有4个元素(索引0-3),n=4时,i=4会访问越界的A[4],导致未定义行为。
  • 节点赋值与next指针的处理逻辑混乱,导致链表的尾节点结构异常。

2. count函数的计数错误

  • 循环条件while(p->next!=NULL)会跳过最后一个节点的计数,比如实际有4个节点,该函数会返回3,因为最后一个节点的next为NULL,不会进入循环。正确逻辑应该是遍历到p本身为NULL时停止。

3. Display函数的显示错误

  • 同样使用while(p->next!=NULL),导致最后一个节点的数据无法被打印,遗漏了尾部元素。

4. insert尾部插入的逻辑错误

  • 由于count函数返回值错误,index==count(first)的判断逻辑失效;同时遍历到尾节点的循环条件i<index无法定位到真正的尾节点,导致新节点无法正确挂载。

修复后的完整代码

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

typedef struct Node
{
    int data;
    struct Node *next;
}Node;
Node *first=NULL;

void create(int A[],int n)
{
    // 创建第一个节点
    first=(Node *)malloc(sizeof(Node));
    first->data=A[0];
    first->next=NULL;
    Node *last = first;

    // 循环创建剩余节点
    for(int i=1;i<n;i++)
    {
        Node *temp=(Node *)malloc(sizeof(Node));
        temp->data=A[i];
        temp->next=NULL;
        last->next=temp;
        last=temp;
    }
}

int count(Node *p)
{
    int count=0;
    // 遍历所有节点,直到p为NULL
    while(p!=NULL)
    {
        count++;
        p=p->next;
    }
    return count;
}

void Display(Node *p)
{
    // 遍历所有节点并打印
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}

void insert(int index, int key)
{
    int nodeCount = count(first);
    if(index<0 || index>nodeCount)
    {
        printf("\nErr... Invalid index");
        return;
    }

    Node *newNode=(Node *)malloc(sizeof(Node));
    newNode->data=key;
    newNode->next=NULL;

    if(index==0)
    {
        newNode->next=first;
        first=newNode;
    }
    else
    {
        Node *temp=first;
        // 遍历到目标索引的前一个节点
        for(int i=0;i<index-1;i++)
        {
            temp=temp->next;
        }
        newNode->next=temp->next;
        temp->next=newNode;
    }
}

int main()
{ 
    int A[]={1,3,5,12};
    create(A,sizeof(A)/sizeof(A[0]));
    insert(4,10);
    Display(first);
    return(0);
}

修复后测试验证

执行insert(4,10)后,输出为1 3 5 12 10,符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 19:05:35