我需要创建一个节点链接列表,以便主要功能能够正常工作;我无法在主要功能中进行任何更改。我是C语言的新手,所以我可能犯了一个或两个简单的错误,并且在尝试运行代码时遇到分段错误。我缺少明显的东西吗?
分段错误发生在标记的行上
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
char *value;
struct Node *next;
}Node;
typedef struct Node** List;
Node *new_node(char *input);
void delete_node(Node *input);
void push(List list,char *value);
List new_list();
Node *new_node(char *input) {
Node *new;
new = malloc(sizeof(Node));
new->value = input;
return new;
}
void delete_node(Node *input) {
free(input);
}
void push(List head,char *input) {
if (*head == NULL) {
*head = new_node(input);
}
else {
Node *new = new_node(input);
while ((*head)->next != NULL) {
*head = (*head)->next;
}
(*head)->next = new;
}
}
List new_list() {
List list = malloc(sizeof(List));
*list = NULL;
return list;
}
int main( void ) {
List list = new_list();
push(list,"First!\n");
push(list,"Second!\n");
push(list,"Third!\n");
push(list,"Fourth!");
printf("%s",(*list)->value);
printf("%s",(*list)->next->value);
printf("%s",(*list)->next->next->value); //Segmentation fault
printf("%s",(*list)->next->next->next->value);
return 0;
}
当我用gdb运行它时,我收到消息:
Third!
Program received signal SIGSEGV,Segmentation fault.
0x0000000000400752 in main () at main.c:54
54 printf("%s",(*list)->next->next->value);