问题描述
输入若干学生的信息(学号、姓名、成绩),当输入学号为 0 时结束,用单向链表组织这些学生信息后,再按序输出
输入:
1001,张三,90,1002,李四,85,0
输出:
1001 张三 90 1002 李四 85
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node * next;
};
int n;
int main() {
struct stud_node *head,*tail,*p;
n=0;
int num,score;
char name[20];
int size = sizeof(struct stud_node);
head=NULL;
tail=p=(struct stud_node *)malloc(size);
printf("input num,name and score:\n");
scanf("%d",&p->num);
while(p->num != 0) {
scanf("%s %d",&p->name,&p->score);
n=n+1;
if(n==1)head=p;
else tail->next=p;
tail=p;
p=(struct stud_node *)malloc(size);
scanf("%d",&p->num);
}
tail->next=NULL;
for(p=head; p!= NULL; p=p->next)
printf("%d %s %d\n",p->num,p->name,p->score);
return 0;
}