本题要求将键盘输入的学生信息创建成单向链表,并在学生信息链表中查找学生信息。
学生信息链表结点定义如下:
typedef struct List{
int sno;
char sname[10];
List *next;
};
需要创建的函数包括:
创建学生信息链表函数:CreateList
查找学生信息链表函数:Find
函数接口定义:
List * CreateList(); //键盘输入若干学生学号和姓名,学号与姓名以空格符间隔,当输入的学号为-1时,输入结束,创建学生信息链表函数,返回学生链表的头指针。
List * Find(List *head, int no) //在学生信息链表(头指针为head)中查询学号为no的学生,返回该学生结点的指针。
裁判测试程序样例:
int main(void){
List *list=NULL,p;
int no;
list=CreateList();
while(~scanf("%d", &no))
{
p=Find(list,no);
if( p ) printf("%s\n", p->sname);
else printf(“Not Found!\n”);
}
return 0;
}
/ 请在这里填写答案 */
输入样例1:
20180002 王红
20180006 张勇
20180008 吴涛
20170010 林玉
-1 xx
20180002
输出样例1:
在这里给出相应的输出。例如:
王红
输入样例2:
20180002 王红
20180006 张勇
20180008 吴涛
20170010 林玉
-1 xx
20170015
输出样例2:
Not Found!
List * CreateList()
{
List *p, *head, *tail;
int id; char name[10];
head = (List*)malloc(sizeof(List));
head->next = NULL;
tail = head;
scanf("%d", &id);
while (id != -1)
{
p = (List*)malloc(sizeof(List));
p->next = NULL;
p->sno = id;
scanf("%s", p->sname);
scanf("%d", &id);
tail->next = p;
tail = p;
}
scanf("%s", name);
return head;
}
List * Find(List *head, int no)
{
List *p;
p = head->next;
while (p->sno != no)
{
if (p->next == NULL)
return p->next;
p = p->next;
}
return p;
}