在C语言中实现链表时的分段错误

在C语言中实现链表时的分段错误,c,linked-list,C,Linked List,我试图创建一个简单的链表,并在列表的末尾插入一个节点。我遇到了一个分割错误 #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node *link; }; void create(struct node *head){ struct node* second = NULL; struct node* last = NULL; second

我试图创建一个简单的链表,并在列表的末尾插入一个节点。我遇到了一个分割错误

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

struct node{
    int data;
    struct node *link;
};

void create(struct node *head){
    struct node* second = NULL;
    struct node* last = NULL;

    second = (struct node*)malloc(sizeof(struct node));
    last = (struct node*)malloc(sizeof(struct node));

    second -> data = 2;
    last -> data = 3;

    head -> link = second;
    second -> link = last;
    last -> link = NULL;
}

void insert_ending(struct node *head){
    struct node* temp = NULL;
    struct node* temp1 = NULL;

    temp1 = (struct node*)malloc(sizeof(struct node));
    temp1 -> data = 5;
    temp1 -> link = NULL;

    temp = head;
    while(temp != NULL){
       temp = temp -> link;
    }temp -> link = temp1;
}

void PrintList(struct node *head){
    while( head != NULL ){
        printf(" %d -->", head -> data);
        head = head -> link;
    }
    printf("\n");
}

int main(){
    struct node* head = NULL;
    head = (struct node*)malloc(sizeof(struct node));
    head -> data = 1;
    head -> link = NULL;

    create(head);
    PrintList(head);

    insert_ending(head);
    PrintList(head);
    return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*链接;
};
创建空(结构节点*头){
结构节点*second=NULL;
结构节点*last=NULL;
第二个=(结构节点*)malloc(sizeof(结构节点));
last=(结构节点*)malloc(sizeof(结构节点));
第二->数据=2;
最后->数据=3;
头->链接=秒;
第二->链接=最后一个;
last->link=NULL;
}
无效插入\u结束(结构节点*头){
结构节点*temp=NULL;
结构节点*temp1=NULL;
temp1=(结构节点*)malloc(sizeof(结构节点));
temp1->data=5;
temp1->link=NULL;
温度=水头;
while(temp!=NULL){
温度=温度->链接;
}temp->link=temp1;
}
无效打印列表(结构节点*头){
while(head!=NULL){
printf(“%d-->”,head->data);
头=头->链接;
}
printf(“\n”);
}
int main(){
结构节点*head=NULL;
head=(结构节点*)malloc(sizeof(结构节点));
头部->数据=1;
head->link=NULL;
创建(头);
印刷品清单(标题);
插入尾端(头部);
印刷品清单(标题);
返回0;
}
我遇到了一个分割错误。输出如下

1-->2-->3--> 分段故障(堆芯转储)


在插入函数中,您需要更改为:

 temp = head;
    while(temp -> link != NULL){
       temp = temp -> link;
    }
    temp -> link = temp1;

原因是,当您使用while-until-temp==null循环时,之后无法执行:temp->link,因为temp是allready-null。

在插入函数中,您需要更改为:

 temp = head;
    while(temp -> link != NULL){
       temp = temp -> link;
    }
    temp -> link = temp1;

原因是,当使用while-until-temp==null循环时,之后无法执行:temp->link,因为temp是allready-null。

在while循环的函数“insert\u ending”中,您要更改 条件来自:

致:


因为现在一旦循环完成,temp为NULL,然后您尝试取消引用它(一个NULL指针)并得到一个错误。

在while循环的函数“insert\u ending”中,您想更改 条件来自:

致:


因为现在一旦循环完成,temp为NULL,然后您尝试取消引用它(NULL指针)并得到一个错误。

您不认为您正在修改头指针吗?如果您使用的是关于第一个节点、最后一个节点的所有特殊情况,空列表等走开你不认为你在修改头指针吗?如果你使用了一个关于第一个节点、最后一个节点、空列表等的所有特殊情况走开我何时使用(temp->link!=NULL)以及何时使用(temp!=NULL),你在插入结束函数中使用它,而不是在那里的for while循环中。在本例中不是,在一般情况下,我应该在哪里使用它?这没有规则,你需要注意什么时候你需要提交到列表的最后一个节点(树等)或者提交到之前的节点。如果您是编程新手,我建议您在任何时候必须处理此类情况,在一些纸上绘制场景,您将自己看到(它对我有用:-)我何时使用(temp->link!=NULL)以及何时使用(temp!=NULL)您正在插入函数中使用它,这里不是for while循环。在本例中不是,在一般情况下,我在哪里使用它?这没有规则,您需要注意何时需要提交到列表的最后一个节点(树等)或提交到之前的节点。如果您是编程新手,我建议您在任何时候必须处理此类情况,在一些纸上绘制场景,您将自己看到它(它对我有用:-)
while ( temp->link != NULL )