【发布时间】:2014-04-21 13:33:58
【问题描述】:
我的目标是创建一个对 List 执行反向操作并返回反向 List 的函数。
Root --> First element in the List,
size --> Size of the list,
str --> Data (string) in the list and
next --> Points to next node in the List.
问题是我正在转储分段错误核心。
请帮我解决这个问题。
提前致谢
typedef struct {
element *root;
int size;
} list;
typedef struct _element {
char* str;
struct _element *next;
} element;
list* reverse_list(list *lst) {
lst = malloc(sizeof(list));
element *aux1, *aux2, *aux3;
aux1 = malloc(sizeof(element));
aux2 = malloc(sizeof(element));
aux3 = malloc(sizeof(element));
aux1 = lst->root;
aux2 = NULL;
while (aux1->next != NULL) {
aux3 = aux1->next;
aux1->next = aux2;
aux2 = aux1;
aux1 = aux3;
}
lst->root = aux1;
return lst;
}
【问题讨论】:
-
你为什么要mallocing任何东西?
-
由于
element *root,您的代码甚至无法编译,因为元素是在之后声明的。 -
我建议尝试使用 gdb 调试它。您将知道确切的位置以及导致分段错误的原因
标签: c function struct linked-list segmentation-fault