任务要求
根据提示,在右侧编辑器的Begin-End
区域内补充代码,完成“成绩查询”模块功能。void queryByScore(struct STUDENT *head,int s);
输入要求
输入内容包含两部分内容,第一部分内容通过struct STUDENT *createList()
输入学生的姓名和成绩,实现链表创建;第二部分输入一个整数score
,表示需要查询的成绩。
输出要求
若查询成功输出学生姓名,否则输出“not found”(不包含双引号)。如果存在多名同学的成绩相同时,则输出第一个成绩为score
的学生姓名。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct STUDENT
{
char name[20];
int score;
struct STUDENT *next;
};
struct STUDENT *createList(); //创建链表
void outputLinkedList(struct STUDENT *head); //遍历链表
void queryByScore(struct STUDENT *head,int s); //成绩查询
void sysMenu(); //系统菜单
int main()
{
struct STUDENT *head;
sysMenu();
head=createList();
outputLinkedList(head);
int s;
scanf("%d",&s);
queryByScore(head,s);
return 0;
}
struct STUDENT *createList() //定义创建链表函数
{
struct STUDENT *head,*p,*q;
char name[20];
/**********Program**********/
head=(struct STUDENT*)malloc(sizeof(struct STUDENT));
q=head;
/********** End **********/
printf("Please input student's name('none' means end of input):");
scanf("%s",name);
while(strcmp(name,"none")!=0)
{
/**********Program**********/
p=(struct STUDENT*)malloc(sizeof(struct STUDENT));
q->next=p;
q=p;
strcpy(q->name,name);
printf("Please input %s's score:",name);
scanf("%d",&(q->score));
printf("Please input student's name('none' means end of input):");
scanf("%s",name);
/********** End **********/
}
q->next=NULL;
return head;
}
void outputLinkedList(struct STUDENT *head) //定义遍历链表函数
{
struct STUDENT *p;
printf("\noutput links'node name and score:\n");
for(p=head->next; p!=NULL; p=p->next)
{
printf("Name is %s,Score is %d\n",p->name,p->score);
}
}
void queryByScore(struct STUDENT *head,int s) //定义成绩查询函数
{
struct STUDENT *p;
/**********Program**********/
int flag=0;
p=head;
while(1){
p=p->next;
if(p->score==s){
flag=1;
break;
}
if(p->next==NULL){
break;
}
}
if(flag){
printf("%s",p->name);
}
else{
printf("not found");
}
/********** End **********/
}
void sysMenu() //定义系统菜单函数
{
printf("====================\n");
printf(" link's operator\n");
printf(" 1. create Link\n");
printf(" 2. forEach Link\n");
printf(" 3. query score\n");
printf(" 4. insert node\n");
printf(" 5. delete node\n");
printf(" 0. exit\n");
printf("====================\n");
}