リストに名前、身長のデータを入力して、ENDで始まる文字列を入力したら終了するプログラム
Posted: 2016年1月31日(日) 18:38
キーボードから名前,年を入力して,それまでに入力した氏名と年齢を年齢の小さい順に表示し,次の入力を促す.ENDで始まる文字を入力すると終了する.
というプログラムを作りたいのですが、並べ替えて表示するのはできたのですが、ENDを入力しても終了させることができません。
何が間違っているのか教えてください。
というプログラムを作りたいのですが、並べ替えて表示するのはできたのですが、ENDを入力しても終了させることができません。
何が間違っているのか教えてください。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct list{
char name[20];
int age;
struct list *next;
};
int main(void){
struct list *head;
struct list *dummy, *new, *prev, *current,*i;
dummy = (struct list *)malloc(sizeof(struct list));
strcpy(dummy->name, "");
dummy->age=0;
dummy->next=NULL;
head=dummy;
char end[3] = "END";
int ret;
while((new = (struct list *)malloc(sizeof(struct list)) )){
printf("氏名と年齢をスペースで区切って入力してください\n");
if( scanf("%s%d", new->name, &new->age)==EOF ) break;
ret = strncmp(new->name, end , 3);
if(ret == 0) break;
current = head;
prev = head;
while( current!=NULL){
prev = current;
current = current->next;
}
new->next = current;
prev->next = new;
int tmp;
char str[20];
i = head;
while(i->next != NULL){
if((head->next)->age > new->age){
new->next = head->next;
prev->next = current;
head->next = new;
}
else if((i->age < new->age) && (new->age <= (i->next)->age)){
prev->next = current;
new->next = i->next;
i->next = new;
}
i = i->next;
}
new = head;
new = new->next;
printf("現在のリスト\n");
while(new != NULL){
printf("%s %d\n",new->name, new->age);
new = new->next;
}
}
}