我很绝望,因为这段代码有时会给我分段错误,我也不知道为什么。实际上,仅应添加一些链接的列表注释,将其打印出来,然后通过释放内存来清空列表。
struct int_list {
int value;
struct int_list *next;
};
typedef struct int_list IntList;
void list_print(IntList *start)
{
IntList *cur = start;
while(cur != NULL)
{
printf("%d\n", cur->value);
cur = cur->next;
}
}
void list_append(IntList **start, int newval)
{
IntList *newel = malloc(sizeof(IntList));
newel->value = newval;
newel->next = NULL;
if(*start == NULL)
{
*start = newel;
}
else
{
IntList *cur = *start;
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = newel;
}
}
void list_free(IntList *start)
{ …Run Code Online (Sandbox Code Playgroud) c memory-management linked-list segmentation-fault singly-linked-list