题目描述
输入若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束,用单向链表组织这些学生信息后,再按顺序输出。
输入描述:
输入多行,直到一行开始输入0结束,每行包括,学号(长度小于等于9),姓名(长度小于等于100),成绩,空格分隔。
输出描述:
按照输入顺序输出每位学生信息,一行一个学生信息,学号,姓名,成绩,空格分隔。
示例1
输入
复制1 linchengda 92 2 chenjiatai 88 3 caoyuxuan 90 4 limengqing 86 0
1 linchengda 92 2 chenjiatai 88 3 caoyuxuan 90 4 limengqing 86 0
输出
复制1 linchengda 92 2 chenjiatai 88 3 caoyuxuan 90 4 limengqing 86
1 linchengda 92 2 chenjiatai 88 3 caoyuxuan 90 4 limengqing 86
#include <stdio.h>
#include <malloc.h>
struct node
{
char name[10];
int num,score;
struct node *next;
};
struct node *create()
{
struct node *Head,*p,*tail;
int date;
Head = (struct node *)malloc(sizeof(struct node));
Head->next = NULL;
tail = Head;
p = (struct node *)malloc(sizeof(struct node));
p->next = NULL;
while(scanf("%d",&date) != EOF)
{
if(date == 0) break;
p->num = date;
scanf("%s %d",p->name,&p->score);
tail->next = p;
tail = p;
p = (struct node *)malloc(sizeof(struct node));
p->next = NULL;
}
return Head;
}
void print(struct node *Head)
{
struct node *p;
p = Head->next;
while(p != NULL)
{
printf("%d %s %d\n",p->num,p->name,p->score);
p = p->next;
}
}
int main()
{
struct node *head;
head = create();
print(head);
return 0;
}