YTU 2975: 我的编号
2975: 我的编号
时间限制: 1 Sec
内存限制: 128 MB
提交: 42
解决: 15
题目描述
建立一个学生链表,每个链表结点含有学生的基本信息,编号和姓名。现在n个学生站成一列,根据学生姓名查找学生的编号。
请将代码补充完整,只需提交补充部分。
请用C++方式提交
C++代码: #include #include 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;inext=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<number<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行,输入要查找的学生姓名。
输出
要寻找学生的编号
样例输入
51001 tom1002 bob1003 mike1006 daming1007 xiaohongtom
样例输出
1001
你 离 开 了 , 我 的 世 界 里 只 剩 下 雨 。 。 。
#include #include 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; inext=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<number<next; }}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;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~