【发布时间】:2014-10-04 14:19:48
【问题描述】:
我从早上开始就一直在尝试修复此代码,但可以完成。所以,最后我需要一些帮助来找出错误。代码编译没有错误,但是当我从终端运行它时,我收到一条错误消息“分段错误:11”
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *head;
void Insert(int data);
void Print();
int main()
{
head = NULL; //list is empty
Insert(3);
Insert(5);
Insert(2);
Insert(8);
Print();
return 0;
}
void Insert(int data)
{
struct Node *temp = malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
struct Node *temp1 = head;
while(temp1 != NULL)
{
temp1= temp1->next;
}
temp1->next = temp;
}
void Print()
{
struct Node *temp =head;
while(temp != NULL)
{
temp = temp->next;
printf("%d", temp->data);
}
}
【问题讨论】:
-
崩溃在哪一行?
-
temp1->next = temp;:temp1是NULL
标签: c linked-list