在链接列表的前面插入节点 #包括 #包括 #包括“list.h” 无效列表插入标题(列表l,int val){ 节点启动; start=calloc(1,sizeof(node)); 开始->值=val; 开始->下一步=l->头部; l->head=开始; 节点电流; while(当前!=NULL){ printf(“数据为%d\n”,当前->值); 当前=当前->下一步; } } int main(int argc,char*argv[]){ List new=calloc(1,sizeof(List)); Node first=calloc(1,sizeof(Node)); Node second=calloc(1,sizeof(Node)); Node third=calloc(1,sizeof(Node)); 第一->值=10; 新建->头部=第一个; 第二->值=20; 第三->值=30; 第一次->下一次=第二次; 第二->下一个=第三个; 第三->下一步=空; listInsertHead(新的,5个); 返回0; } typedef结构_节点*节点; 类型定义结构列表*列表; 类型定义结构列表{ 节点头; }名单; 类型定义结构节点{ int值; 节点下一步; }节点;
我试图在链表的开头添加一个新节点作为头。然而,我不断地遇到分割错误,所以我不确定该怎么办。结构位于代码的底部。所以我想做的是: 1->2->3->NULL在链接列表的前面插入节点 #包括 #包括 #包括“list.h” 无效列表插入标题(列表l,int val){ 节点启动; start=calloc(1,sizeof(node)); 开始->值=val; 开始->下一步=l->头部; l->head=开始; 节点电流; while(当前!=NULL){ printf(“数据为%d\n”,当前->值); 当前=当前->下一步; } } int main(int argc,char*argv[]){ List new=calloc(1,sizeof(List)); Node first=calloc(1,sizeof(Node)); Node second=calloc(1,sizeof(Node)); Node third=calloc(1,sizeof(Node)); 第一->值=10; 新建->头部=第一个; 第二->值=20; 第三->值=30; 第一次->下一次=第二次; 第二->下一个=第三个; 第三->下一步=空; listInsertHead(新的,5个); 返回0; } typedef结构_节点*节点; 类型定义结构列表*列表; 类型定义结构列表{ 节点头; }名单; 类型定义结构节点{ int值; 节点下一步; }节点;,c,list,C,List,我试图在链表的开头添加一个新节点作为头。然而,我不断地遇到分割错误,所以我不确定该怎么办。结构位于代码的底部。所以我想做的是: 1->2->3->NULL 新建(节点)->1->2->3->NULL您的错误来自于在未初始化当前变量的情况下尝试打印列表: #include <stdio.h> #include <stdlib.h> #include "list.h" void listInsertHead (List l, int val) { Node star
新建(节点)->1->2->3->NULL您的错误来自于在未初始化当前变量的情况下尝试打印列表:
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void listInsertHead (List l, int val) {
Node start;
start = calloc(1, sizeof(node));
start->value = val;
start->next = l->head;
l->head = start;
Node current;
while (current != NULL) {
printf("data is %d\n", current->value);
current = current->next;
}
}
int main(int argc, char *argv[]) {
List new = calloc(1, sizeof(list));
Node first = calloc(1, sizeof(node));
Node second = calloc(1, sizeof(node));
Node third = calloc(1, sizeof(node));
first->value = 10;
new->head = first;
second->value = 20;
third->value = 30;
first->next = second;
second->next= third;
third->next = NULL;
listInsertHead(new, 5);
return 0;
}
typedef struct _node *Node;
typedef struct _list *List;
typedef struct _list {
Node head;
} list;
typedef struct _node {
int value;
Node next;
} node;
您尚未将其设置为任何值,但正在尝试循环-您需要初始化它:
Node current;
while (current != NULL) {
printf("data is %d\n", current->value);
current = current->next;
}
您的segfault来自于在未初始化当前变量的情况下尝试打印列表:
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void listInsertHead (List l, int val) {
Node start;
start = calloc(1, sizeof(node));
start->value = val;
start->next = l->head;
l->head = start;
Node current;
while (current != NULL) {
printf("data is %d\n", current->value);
current = current->next;
}
}
int main(int argc, char *argv[]) {
List new = calloc(1, sizeof(list));
Node first = calloc(1, sizeof(node));
Node second = calloc(1, sizeof(node));
Node third = calloc(1, sizeof(node));
first->value = 10;
new->head = first;
second->value = 20;
third->value = 30;
first->next = second;
second->next= third;
third->next = NULL;
listInsertHead(new, 5);
return 0;
}
typedef struct _node *Node;
typedef struct _list *List;
typedef struct _list {
Node head;
} list;
typedef struct _node {
int value;
Node next;
} node;
您尚未将其设置为任何值,但正在尝试循环-您需要初始化它:
Node current;
while (current != NULL) {
printf("data is %d\n", current->value);
current = current->next;
}
不要将指针隐藏在
typedef
s后面-这会使代码比需要的更难阅读和理解。另外:节点当前代码>-您没有初始化此指针指向任何东西(甚至不是NULL
),因此您在以下行上调用未定义的行为。首先,使用调试器。抱歉,忘记第0件事:启用编译器警告并将其视为错误。不要将指针隐藏在typedef
s后面-这会使代码比需要的更难阅读和理解。另外:节点当前代码>-您没有初始化此指针指向任何东西(甚至不是NULL
),因此您在以下行上调用未定义的行为。首先,使用调试器。抱歉,忘记了第零件事:启用编译器警告并将其视为错误。哦,该死!菜鸟的错误哈哈,我都没意识到。非常感谢你!哦,该死!菜鸟的错误哈哈,我都没意识到。非常感谢你!