我得到这段代码在 list.Segmentation 错误的末尾插入一个元素
I got this code to inset an element at the end of the list.Segmentation fault
大家好,我编写了这段用于在列表末尾插入元素的代码
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
struct node*head;
void insert(int x){
struct node*temp=(node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
struct node*temp1=head;
while(temp1->link!=NULL)
temp1=temp1->link;
temp1->link=temp;
};
void display(){
struct node*temp=head;
printf("LIst is:");
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->link;
}
printf("\n");
};
int main()
{ head=NULL;
int n,i,x;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the elements:");
scanf("%d",&x);
insert(x);
display();
}
}
每次我编译it.It显示
Segmentation fault core dumped
请帮忙
我不知道哪里错了
我正在访问“不属于我的内存。
head
开头是 NULL
。所以当你这样做时:
struct node*temp1=head;
while(temp1->link!=NULL)
^^^^^
您取消引用 NULL
并且程序崩溃。
您需要一个额外的 if
语句来处理 head
为 NULL
的情况。
...
temp->link=NULL;
if (head == NULL)
{
head = temp;
return;
}
struct node*temp1=head;
...
顺便说一句:一般来说,拥有一个全局变量 head
.
是个坏主意
为了避免使用全局变量,您可以做两件事 - return 每次调用 insert
时 head
指针或将 head
的地址传递给功能。我更喜欢最后一个。看起来像:
void insert(struct node** pHead, int x){
struct node* temp=(node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
if (*pHead == NULL)
{
*pHead = temp; // Update head
return;
}
struct node* temp1 = *pHead;
while(temp1->link!=NULL)
temp1=temp1->link;
temp1->link=temp;
};
并这样称呼它:
int main()
{
struct node* head=NULL; // Use a local variable
int n,i,x;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the elements:");
scanf("%d",&x);
insert(&head, x); // Pass the address of head
....
}
}
您将 head
分配给 temp1
struct node*temp1=head;
而此刻head
是NULL
然后你取消引用 temp1
(NULL
)
while(temp1->link!=NULL)
这就是为什么会出现段错误。
大家好,我编写了这段用于在列表末尾插入元素的代码
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
struct node*head;
void insert(int x){
struct node*temp=(node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
struct node*temp1=head;
while(temp1->link!=NULL)
temp1=temp1->link;
temp1->link=temp;
};
void display(){
struct node*temp=head;
printf("LIst is:");
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->link;
}
printf("\n");
};
int main()
{ head=NULL;
int n,i,x;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the elements:");
scanf("%d",&x);
insert(x);
display();
}
}
每次我编译it.It显示
Segmentation fault core dumped
请帮忙 我不知道哪里错了 我正在访问“不属于我的内存。
head
开头是 NULL
。所以当你这样做时:
struct node*temp1=head;
while(temp1->link!=NULL)
^^^^^
您取消引用 NULL
并且程序崩溃。
您需要一个额外的 if
语句来处理 head
为 NULL
的情况。
...
temp->link=NULL;
if (head == NULL)
{
head = temp;
return;
}
struct node*temp1=head;
...
顺便说一句:一般来说,拥有一个全局变量 head
.
为了避免使用全局变量,您可以做两件事 - return 每次调用 insert
时 head
指针或将 head
的地址传递给功能。我更喜欢最后一个。看起来像:
void insert(struct node** pHead, int x){
struct node* temp=(node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
if (*pHead == NULL)
{
*pHead = temp; // Update head
return;
}
struct node* temp1 = *pHead;
while(temp1->link!=NULL)
temp1=temp1->link;
temp1->link=temp;
};
并这样称呼它:
int main()
{
struct node* head=NULL; // Use a local variable
int n,i,x;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the elements:");
scanf("%d",&x);
insert(&head, x); // Pass the address of head
....
}
}
您将 head
分配给 temp1
struct node*temp1=head;
而此刻head
是NULL
然后你取消引用 temp1
(NULL
)
while(temp1->link!=NULL)
这就是为什么会出现段错误。