属虎女86年农历4月19:C语言:编写一个查找数据的功能菜单

来源:百度文库 编辑:高校问答 时间:2024/05/06 11:38:18
该功能可允许用户输入某个同学的学号,并依此学号在链表中进行查找,如果在链表中存在该同学,则打印出该同学的学号和成绩。如果链表中不存在该同学,则给出提示信息。

#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
int num; //学号
int score; //成绩
struct student *next; //节点的next
}stu; //学生信息节点

void main()
{
void creat(stu *);
void select(int,stu*);
void show(stu*); //函数声明
stu *L;
int flag=1,sno;
char choice;
L=(stu*)malloc(sizeof(stu));
L->next=NULL; //初始化链表
creat(L); //创建学生信息链表
show(L); //显示链表中所有学生的信息
while(flag) //控制自动循环查找
{
printf("do you want to sele\n");
getchar(); //吸收回车符
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("input the num\n");
scanf("%d",&sno);
select(sno,L);
} //用户要查找(输入'y;或者'Y'),查找学生信息
else
{
printf("select is over\n");
flag=0;
} //用户不要求查找,则退出程序
}
}

void creat(stu *L)
{
stu *r;
int number,score,flag=1;
char choice;
printf("please input the infor of student\n");
while(flag) //控制循环输入
{
printf("do you want to creat\n");
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("number:");
scanf("%d",&number);
printf("score:");
scanf("%d",&score); //输入学生信息
r=(stu*)malloc(sizeof(stu));
r->num=number;
r->score=score;
r->next=L->next;
L->next=r;
getchar();
} //用头插法将学生信息链入表中
else
{
printf("input over\n");
flag=0;
} //输入结束

}
}

void select(int number,stu *L)
{
stu *p;
p=L->next;
while(p!=NULL&&p->num!=number)//链表未结束并且未找到信息
p=p->next; //遍历链表查找对应学号
if(p->num==number)
{
printf("the infor of this stu is:\n");
printf("num:%d,score:%d\n",p->num,p->score);
} //找到对应学号,则输出节点内容
else if(p==NULL)
printf("can not find\n");
}//查找学号 //未找到学号信息

void show(stu *L)
{
stu *p;
p=L->next;
while(p!=NULL) //链表未结束
{
printf("num:%d,score:%d",p->num,p->score);//输出链表中内容
p=p->next; //指针后移
}
printf("\n");
}//显示链表中内容

程序在VC6.0中调试通过!按照提示输入信息即可