我的编号

2020年1月17日 986点热度 0人点赞 0条评论

我的编号

时间: 1ms        内存:128M

描述:

建立一个学生链表,每个链表结点含有学生的基本信息,编号和姓名。现在n个学生站成一列,根据学生姓名查找学生的编号。

请将代码补充完整,只需提交补充部分。

请用C++方式提交

C++代码:

#include <iostream>
#include <string.h>
using namespace std;
struct student
{
    int number;
    char name[20];
    student *next;
};
student *createlist(int n)
{
    int i;
    student *head=NULL;
    student *p=NULL;
    head=new student;
    head->next=NULL;
    p=head;
    cin>>p->number>>p->name;
    for(i=1;i<n;i++)
    {
        p->next=new student;
        p=p->next;
        p->next=NULL;
        cin>>p->number>>p->name;
    }
    return head;
}

void searchstu(student *head,char *str)
{
    student *current;
    current=head;
    while(current!=NULL)
    {
        if(!strcmp(current->name,str))
        {
            cout<<current->number<<endl;
            break;
        }
        /*
        补充部分,当前结点后移
        */
    }
}

void destroy(student *head)
{
    student *p;
    p=head;
    while(head!=NULL)
    {
        p=head;
        head=head->next;
        delete p;
    }
}

int main()
{
    int n;
    char str[20];
    student *head;
    cin>>n;
    head=createlist(n);
    cin>>str;
    searchstu(head,str);
    destroy(head);
    return 0;
}

输入:

第1行输入一个n,表示n个学生;
第2行到第n+1行,每行输入一个学生的编号和姓名,以空格隔开;
最后1行,输入要查找的学生姓名。

输出:

要寻找学生的编号

示例输入:

5
1001 tom
1002 bob
1003 mike
1006 daming
1007 xiaohong
tom

示例输出:

1001

提示:

参考答案:

解锁文章

没有看到答案?微信扫描二维码可免费解锁文章

微信扫描二维码解锁

使用微信扫描二维码打开广告页面后可以立即关闭,再刷新此页面即可正常浏览此文章

所跳转广告均由第三方提供,并不代表本站观点!

已经扫描此二维码?点此立即跳转

code

这个人很懒,什么都没留下

文章评论