リンクされたリストに関する次のコードについて助けが必要です。
#include <stdlib.h>
#include <stdio.h>
struct nodo {
int d;
struct nodo *next;
};
struct nodo *full();
int main()
{
struct nodo *l;
/* l=(struct nodo *)malloc(sizeof(struct nodo)); */
l = full();
while(l!=NULL) {
printf("-->%d\n", l->d);
l =l->next;
}
system("PAUSE");
}
struct nodo *full()
{
int i;
struct nodo *head, *nes;
head = (struct nodo *)malloc(sizeof(struct nodo));
head->next = NULL;
for(i = 1; i < 5; i++) {
nes = (struct nodo *)malloc(sizeof(struct nodo));
printf("Insert the %d element:\n", i);
scanf("%d", &nes->d);
nes->next = head;
head = nes;
}
return head;
}
たとえば、入力しようとすると1, 2, 3, 4
、次の出力が得られます。
-->4
-->3
-->2
-->1
-->9708864
最後の番号を取得するのはなぜですか? コードの何が問題になっていますか?