为什么最后一次追加电话不起作用?我必须在这里添加一些垃圾,因为它抱怨我的帖子主要是代码,我希望它现在已经足够了。
typedef struct node {
int val;
struct node * next;
} node_t;
void append_node(node_t * head, int val) {
node_t * current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(node_t));
if(current->next == NULL)
printf("err");
current = current->next;
current->val = val;
current->next = NULL; //malloc(sizeof(node_t));
}
void print_list(node_t * head) {
node_t * current = head;
while(current->next != NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
int main() {
node_t * list = malloc(sizeof(node_t));
list->val = 1;
list->next = NULL;
append_node(list,12);
append_node(list,14);
append_node(list,17);
print_list(list);
return 0;
}
输出:
1 12 14