这个链表哪里错了?

来源:百度知道 编辑:UC知道 时间:2024/05/16 20:18:16
#define S struct workers
#define LEN sizeof(S)
#include <stdio.h>
#include <stdlib.h>

struct workers{
int num,score;
char name[10];
S *next;
}

main()
{
S *p,*q,*head;
char ch;
p=q=head=(S*)malloc(LEN);
while(1){
printf("Num:");
scanf("%d",&p->num);
printf("Name:");
scanf("%s",p->name);
printf("Score:");
scanf("%d",&p->score);
p=(S*)malloc(LEN);
printf("Continue ? ( y or n ):");
ch=getchar();
if(ch=='y')break;
else
q->next=p;
q=p;
}
q->next=0;
p=head;
while(p){
printf("Num:%d Name:%s Score:%d\s",p->num,p-

>name,p->score);
p=p->next;
}
}

代码中主要是ch=getchar();这一句出问题了
修改如下:
#define S struct workers
#define LEN sizeof(S)
#include <stdio.h>
#include <stdlib.h>

struct workers{
int num,score;
char name[10];
S *next;
}

main()
{
S *p,*q,*head;
char ch;
p=q=head=(S*)malloc(LEN);
while(1){
printf("Continue ? ( y or n ):");
ch=getch();
printf("%c\n",ch);
if(ch=='n'){break;}
else if(ch=='y')
{
p=(S*)malloc(LEN);
printf("Num:");
scanf("%d",&p->num);
printf("Name:");
scanf("%s",&p->name);
printf("Score:");
scanf("%d",&p->score);
q->next=p;
q=q->next;
q->next=NULL;
}
}
q->next=NULL;
p=head->next;
printf("\n\nThe result :\n");
while(