Utk*_*jal 1 c linked-list data-structures
这是我用ANSI C编写的代码.我经常遇到运行时错误:Segmentation Fault(SIGSEGV).请帮帮我.我是数据结构和C的新手.我无法检测到问题.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *nxt;
}node;
node * create(int n);
void display(node *head);
int main()
{
int n = 0;
node *head = NULL;
printf("Enter the number of nodes\n");
scanf("%d", &n);
head = create(n);
display(head);
return 0;
}
node * create(int n)
{
int i;
node *head = NULL;
node *temp = NULL;
node *p = NULL;
for (i = 0; i < n; i++)
{
temp = (node *)malloc(sizeof(node));
printf("\nEnter the value of %d node", i + 1);
scanf("%d", &temp->data);
temp->nxt = NULL;
if (head == NULL)
{
head = temp;
}
else
{
p = head;
while (p->nxt != NULL)
{
p = p->nxt;
p->nxt = temp;
}
}
}
return head;
}
void display(node *head)
{
node *p = NULL;
if (head = NULL)
{
printf("\nEmpty List");
}
else
{
p = head;
while (p != NULL);
{
printf("%d->", p->data);
p = p->nxt;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Thomas Jager在他的回答中给了你一个重要的解决方案.我在评论中给了你两个重要的修正.当这些结合起来时,代码对我有用.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *nxt;
} node;
node *create(int n);
void display(node *head);
static void error(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int main(void)
{
int n = 0;
node *head = NULL;
printf("Enter the number of nodes: ");
if (scanf("%d", &n) != 1)
error("failed to read an integer");
head = create(n);
display(head);
return 0;
}
node *create(int n)
{
int i;
node *head = NULL;
node *temp = NULL;
node *p = NULL;
for (i = 0; i < n; i++)
{
temp = (node *)malloc(sizeof(node));
if (temp == NULL)
error("failed to allocate memory");
printf("\nEnter the value of %d node: ", i + 1);
if (scanf("%d", &temp->data) != 1)
error("failed to read an integer");
temp->nxt = NULL;
if (head == NULL)
{
head = temp;
}
else
{
p = head;
while (p->nxt != NULL)
{
p = p->nxt;
}
p->nxt = temp;
}
display(head); // Debugging - check the list as it is built
}
return head;
}
void display(node *head)
{
node *p = NULL;
if (head == NULL)
{
printf("Empty List\n");
}
else
{
p = head;
while (p != NULL)
{
printf("%d->", p->data);
p = p->nxt;
fflush(stdout); // Band-aid - remove ASAP
}
putchar('\n');
}
fflush(stdout); // Band-aid - remove ASAP
}
Run Code Online (Sandbox Code Playgroud)
我使用display输入代码中的函数来确保列表始终干净利落.它占下面列表的额外副本.该代码还使用换行符终止输出行,这有助于确保它出现.有两个记录的"删除我"调用fflush(stdout)是不需要的,但在调试代码崩溃时有用.有一个部分论点是提示printf()调用应该跟着a后面fflush(stdout)以确保出现提示.通常不需要交互式输出.
请注意,我添加了错误报告功能,以便于报告错误,因此鼓励您检测可能的错误.你可以看到我的首选的错误处理代码在我SOQ在GitHub上(堆栈溢出问题)存储库作为文件stderr.c,并stderr.h在SRC/libsoq子目录.
当我使用数据结构(例如列表)时,我通常会创建一个dump_list()函数.通常有2或3个参数:
void dump_list(const char *tag, const node *list);
void dump_list(FILE *fp, const char *tag, const node *list);
Run Code Online (Sandbox Code Playgroud)
'tag'参数用于注释输出:
dump_list(__func__, head); // In create()
dump_list("result", head); // In main()
Run Code Online (Sandbox Code Playgroud)
标签很重要; 它允许你创建一个独特的标记每个地方使用功能的地方(我用dump_list("point 1", …),dump_list("point 2", …)......在许多场合一个函数内).如果我认为我可能需要它去除标准输出以外(例如标准错误或日志文件),我自己提供带FILE *参数的版本.使用此功能可以检查数据结构.请注意,不允许该函数修改数据结构.您可能对不同的格式display()从函数dump_list()功能-在这种情况下,你不能调用dump_list()的main()函数.但是,有这样的功能来验证您的数据结构可以帮助很大.
使用显示的代码,我可以运行程序(使用GCC 8.1 set fussy ll53创建ll53.c,编译干净),如下所示:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes \
> -Wstrict-prototypes ll53.c -o ll53
$ ./ll53
Enter the number of nodes: 1
Enter the value of 1 node: 234
234->
234->
$ ./ll53
Enter the number of nodes: 2
Enter the value of 1 node: 234
234->
Enter the value of 2 node: 123
234->123->
234->123->
$ ./ll53
Enter the number of nodes: 7
Enter the value of 1 node: 987
987->
Enter the value of 2 node: 888
987->888->
Enter the value of 3 node: 789
987->888->789->
Enter the value of 4 node: 345
987->888->789->345->
Enter the value of 5 node: 444
987->888->789->345->444->
Enter the value of 6 node: 543
987->888->789->345->444->543->
Enter the value of 7 node: 0
987->888->789->345->444->543->0->
987->888->789->345->444->543->0->
$ ./ll53
Enter the number of nodes: 0
Empty List
$
Run Code Online (Sandbox Code Playgroud)