求C语言大佬,这个程序循环不了,而且输入也有问题,哪里出错了?后面

求C语言大佬,这个程序循环不了,而且输入也有问题,哪里出错了?后面#include<stdio.h>
#include<stdlib.h>
struct link *appendnode(struct link *head);
void displink(struct link *head);
void deletememory(struct link *head);
struct message
{
char name[10];
int score[5];
};
struct link
{
struct message data;
struct link *next;
};
int main()
{
int i=0;
char c;
struct link *head=NULL;
printf("do you want to append a new node(Y/N)?\n");
scanf(" %c",&c);
while (c =='Y')
{
head=appendnode(head);
displink(head);
printf("do you want to append a new node(Y/N)?\n");
scanf(" %c",&c);
i++;
}
printf("%d new nodes have been appended!\n",i);
deletememory(head);
return 0;
}
struct link*appendnode(struct link *head)
{
struct link *p=NULL;
struct link *pr=head;
struct message data;
p=(struct link *)malloc(sizeof (struct link));
if (p==NULL)
{
printf("no enough memory to alloc");
exit(0);
}
if (head==NULL)
{
head=p;
}
else
{
while (pr->next!=NULL)
{
pr=pr->next;
}
pr->next=p;
}
pr=p;
printf("请输入姓名和五科成绩:\n");
scanf("%s%d%d%d%d%d\n",p->data.name,&p->data.score[0],&p->data.score[1],&p->data.score[2],&p->data.score[3],&p->data.score[4]);
pr->next=NULL;
return head;
}
void displink(struct link *head)
{
struct link *p=head;
int j=1;
struct message data;
while (p!=NULL)
{ printf("%5d%s%5d%5d%5d%5d%5d\n",j,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2],p->data.score[3],p->data.score[4]);
p=p->next;
j++;
}}

对程序作了几处修改,现在可以运行了:

#include<stdio.h>
#include<stdlib.h>
struct link *appendnode(struct link *head);
void displink(struct link *head);
void deletememory(struct link *head);
struct message
{
char name[10];
int score[5];
};
struct link
{
struct message data;
struct link *next;
};
int main()
{
int i=0;
char c;
struct link *head=NULL;
printf("do you want to append a new node(Y/N)?\n");
scanf(" %c",&c);
while (c=='Y')
{
head=appendnode(head);
displink(head);
printf("do you want to append a new node(Y/N)?\n");
scanf("%c",&c);
i++;
}
printf("%d new nodes have been appended!\n",i);
//deletememory(head);
return 0;
}
struct link*appendnode(struct link *head)
{
struct link *p=NULL;
struct link *pr=head;
struct message data;
p=(struct link *)malloc(sizeof (struct link));
if (p==NULL)
{
printf("no enough memory to alloc");
exit(0);
}
if (head==NULL)
{
head=p;
}
else
{
while (pr->next!=NULL)
{
pr=pr->next;
}
pr->next=p;
}
pr=p;
printf("请输入姓名和五科成绩:\n");
scanf("%s%d%d%d%d%d%*c",p->data.name,&p->data.score[0],&p->data.score[1],
&p->data.score[2],&p->data.score[3],&p->data.score[4]);
pr->next=NULL;
return head;
}
void displink(struct link *head)
{
struct link *p=head;
int j=1;
struct message data;
while (p!=NULL)
{ printf("%5d%s%5d%5d%5d%5d%5d\n",j,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2],p->data.score[3],p->data.score[4]);
p=p->next;
j++;
}}

温馨提示:答案为网友推荐,仅供参考
相似回答