c语言 - 链表错误"Segmentation Fault"核心转储



在Fedora gcc上尝试以下代码,用于一个简单的链表,将新节点添加到列表的尾部。编译中没有错误。在执行期间,它显示分段错误、核心转储。 在MS Windows上,它正在工作。

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertion(struct Node *);
void display(struct Node *);
int main(void)
{
struct Node *head;
head=NULL;
head->next=NULL;
int choice, cont;
do
{
printf("1.Insert      2.Display       3.Exit");
scanf("%d",&choice);
if(choice==1)
{
insertion(head);
}
else if(choice==2)
{
display(head);
}
else if(choice==3)
{
exit(0);
}
else
{
printf("Wrong choice");
}
printf("Continue? Press 1 otherwise 0:");
scanf("%d",&cont);
}while(cont==1);
return 0;
}
void insertion(struct Node *start)
{
int data;
struct Node *temp=NULL;
temp->next=NULL;
struct Node *mnew=NULL;
mnew->next=NULL;
mnew=(struct Node *)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&data);
mnew->data=data;
if(start==NULL)
{
start=mnew;
}
else if(start!=NULL && start->next==NULL)
{
start->next=mnew;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=mnew;
}
}
void display(struct Node *start)
{
struct Node *temp=NULL;
temp->next=NULL;    
if(start==NULL)
{
printf("nNothing to display!");
}
else if(start!=NULL && start->next==NULL)
{
printf("%d",start->data);
}
else
{
temp=start;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
}

感谢您的帮助。

head=NULL;
head->next=NULL;

这段代码永远无法工作,因为如果它指向 NULL(又名无处),则无法访问或将值分配给head的属性。

仔细看看insertion函数中的这两行:

struct Node *temp=NULL;
temp->next=NULL;

第一个定义指向struct Node的指针,并使其成为空指针。下一行取消引用此空指针,该指针无效并导致未定义的行为

您在多个地方都有相同的问题,既如此,又通常取消引用空指针。

不能使用空指针访问数据。因此,此代码片段(以及类似的代码片段)

struct Node *head;
head=NULL;
head->next=NULL;
^^^^^^^^^^^^^^^

无效。

至于函数insertion那么你必须通过引用传递头部。否则,函数将处理头部的副本,并且函数中头部副本的任何更改都不会影响原始头部。

此外,如果内存分配失败,则希望函数发出有关此的信号。因此,最好使用返回类型int而不是返回类型void

所以函数声明可以看起来像

int insertion( struct Node ** );
^^^            ^^^^^^^^^^^^^^

该函数可以像这样定义

int insertion( struct Node **start )
{
int data;
printf( "Enter data: " );
scanf( "%d", &data );
struct Node *temp = ( struct Node * )malloc( sizeof( struct Node ) );
int success = temp != NULL;
if ( success )
{
temp->data = data;
temp->next = NULL;
while ( *start ) start = &( *start )->next;
*start = temp;
}
return success;
}

可以通过以下方式调用该函数

insertion( &head );

函数display可能如下所示

void display( struct Node *start )
{
if ( start == NULL )
{
printf( "nNothing to display!n" );
}
else 
{
for ( ; start; start = start->next )
{
printf( "%d ", start->data );
}
putchar( 'n' );
}
}

与前面的注释一样,未定义将NULL指针定向到另一个NULL(因为指针应包含地址)。 现在一些建议:

1)像这样定义结构:

typedef struct Node *node_pointer;

这样可以更轻松地为该结构定义指针。

2)

mnew=malloc(sizeof(*mnew)); //this is easier, and this should be before the ..->next=NULL;

还要检查分配是否成功:

if (!mnew)
return; //return something needed

最新更新