リスト構造がうまく動きません!!
C言語で以下のようなプログラムを作りました。
「main関数内で下記のデ-タを構造体に格納し、キーボードから入力された名前と該当する学生の身長と年齢を画面に表示するプログラムを作成せよ。」というものです。
このプログラムはコンパイルは通るのですが、2人目以降のデータを表示させようとしても表示してくれません。。。どうもリスト構造のfor文がうまくループしていないみたいなんですが原因が分かりません。どなたか原因の分かる方アドバイスをお願いしますm(_ _)m
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct data{
char name[20];
int height;
int age;
struct data *next;
}person;
person *newperson(void);
int main(void){
char namae[20],s[20];
int toshi,shinchou,i;
person *head;
person *list;
person *nlist;
person *LIST;
head = newperson();
nlist = head;
printf("データを入れてください。\n");
for(i=0;i<=4;i++){
scanf("%s",namae);
scanf("%d",&shinchou);
scanf("%d",&toshi);
list = newperson();
strcpy(list ->name,namae);
list -> height = shinchou;
list -> age = toshi;
nlist -> next = list;
nlist = list;
}
printf("知りたい人の名前は?\n");
scanf("%s",s);
for(LIST = head->next;LIST ->next != NULL;LIST = LIST->next){
if(strcoll(s,LIST ->name)==0){
printf("%s\t%d\t%d\n",LIST->name,LIST->height,LIST->age);
break;
}
printf("%s\n",LIST->name);
printf("%s\n",LIST->next->name);
}
return(0);
}
person *newperson(){
person *dummy;
dummy = (person*)malloc(sizeof(person));
dummy -> next = NULL;
return(dummy);
}