C で基本的なリンク リストを作成しようとしています。構造体と「追加」関数があります。しかし、いくら項目を追加しても、構造体はまったく変化しません。本当にバグが見つかりません。
構造体:
typedef struct list {
int node;
struct list *next;
} list_t;
追加機能:
void append(list_t *list, int node) {
if(!list) {
list = malloc(sizeof(list_t));
list->node = node;
list->next = NULL;
}else {
list_t *probe = list;
while(probe->next) probe = probe->next;
probe->next = malloc(sizeof(list_t));
probe = probe->next;
probe->node = node;
probe->next = NULL;
}
}
印刷機能:
void lprint(list_t *list) {
if(!list) {
printf("empty");
}else {
list_t *probe = list;
do {
printf("%d ", probe->node);
probe = probe->next;
} while(probe);
}
printf("\n");
}
主な機能:
void main() {
list_t *list = NULL;
int node;
for(node = 0; node < 5; node++) {
append(list, node);
lprint(list);
}
}
出力は次のとおりです。
empty
empty
empty
empty
empty
それはあるべきですが:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
何か助けはありますか?