给出一张学生数据表,如下图所示。请采用链式存储结构建立该学生数据表并输出。
函数接口定义:
struct Student* read_data(); //建立学生信息链表
void output_data(struct Student* head); //输出学生信息
char* find_data(int no, struct Student* head); //查找指定学号的学生姓名
其中 no
为学生学号, head
为链表的头指针。
裁判测试程序样例:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
//链式存储结构结点类型
struct Student
{
int no; //学号
char name[30]; //姓名
char gendre[30]; //性别
char clazz[30]; //班号
struct Student* next;//指向下一个结点的指针
};
struct Student* read_data(); //建立学生信息链表
void output_data(struct Student* head); //输出学生信息
char* find_data(int no, struct Student* head); //查找指定学号的学生姓名
int main(int argc, char const *argv[])
{
struct Student* head;
head = read_data();
output_data(head);
int no;
scanf("%d", &no);
char* pStr = find_data(no, head);
puts(pStr);
return 0;
}
/* 请在这里填写答案 */
输入样例:
1 张斌 男 9901
8 刘丽 女 9902
34 李英 女 9901
20 陈华 男 9902
12 王奇 男 9901
26 董强 男 9902
5 王萍 女 9901
8
输出样例:
1 张斌 男 9901
8 刘丽 女 9902
34 李英 女 9901
20 陈华 男 9902
12 王奇 男 9901
26 董强 男 9902
5 王萍 女 9901
刘丽
答案代码:
struct Student* read_data(){
struct Student *head, *p, *q, *L;
head = q = (struct Student*)malloc(sizeof(struct Student));
head->next = NULL;
for(int i = 0 ; i < 7; i++){
p = (struct Student*)malloc(sizeof(struct Student));
scanf("%d%s%s%s",&p->no,p->name,p->gendre,p->clazz);
q->next = p;
q = p;
}
q->next = NULL;
return head;
}
void output_data(struct Student* head){
struct Student *L = head->next;
while(L){
printf("%d %s %s %s\n",L->no,L->name,L->gendre,L->clazz);
L = L->next;
}
}
char* find_data(int no, struct Student* head){
struct Student *L = head->next;
while(L){
if(L->no == no)
{
return L->name;
}
L = L->next;
}
return 0;
}