建立学生信息链表 (20分)
本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。函数接口定义:void input();单向链表的头尾指针保存在全局变量head和tail中。输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。裁判测试程序样例:#include <stdio.h>#include <stdlib.h>#include <string.h>...
·
本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。
函数接口定义:
void input();
单向链表的头尾指针保存在全局变量head和tail中。
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *head, *tail;
void input();
int main()
{
struct stud_node *p;
head = tail = NULL;
input();
for ( p = head; p != NULL; p = p->next )
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
输出样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
通过代码:
void input(){
struct stud_node *p;
int x;
scanf("%d",&x); //先判断输入是否为0,再分配空间
while(x){
p = (struct stud_node*)malloc(sizeof(struct stud_node));
p -> num = x;
scanf("%s %d", p->name, &p->score);
p -> next = NULL;
if(head == NULL) head = p; //让haed等于p
else tail -> next = p; //将p连接到链表结尾
tail = p; //将tail后移
scanf("%d",&x);
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)