gpt4 book ai didi

c - 为什么在我第一次在代码中插入项目后,我的代码中出现段错误

转载 作者:行者123 更新时间:2023-11-30 14:36:32 25 4
gpt4 key购买 nike

 /*Linked list insertin deletion display

我已经声明了struct node的全局变量*head=NULL;

linkedliSTLinkedliSTLinkedlisr=tlinkedliSTLinkedliSTLinkedlist

linkedliSTLinkedliSTLinkedlisr=tlinkedliSTLinkedliSTLinkedlist

void insert(int x)//i guess the problem might be that temp is pointing to head as well as new  
{
struct node *temp=NULL;
struct node *new;

new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->next=NULL;

if(head==NULL)
{
head=new;
temp=head;
}
else
{
temp->next=new;
temp=new;
}
}

最佳答案

temp->next=new 很可能是您的问题。在函数顶部将 temp 设置为 NULL,并使用 -> 运算符,您将取消引用空指针。

关于c - 为什么在我第一次在代码中插入项目后,我的代码中出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57994693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com