资源描述:
《C语言链表实现学生成绩信息的管理.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*----------------------------------------------//Student.cpp:链表实现对学生成绩的管理------------------------------------------------*/#include#include#includeusingnamespacestd;structStudent{intid;char*name;floatscore;Student*next;};Student*Create();Student*Create(inti,char*ch,floa
2、tsc);voidInsert(Student*h,Student*p);voidOutput(Student*h);Student*Search(Student*h,constintid);voidDelete(Student*h,Student*p);intmain(intargc,char*argv[]){Student*head,*p;head=Create();p=Create(1,"a",83);Insert(head,p);p=Create(2,"b",97);Insert(head,p);p=Create(3,"c",64);Insert(head,p);p=Create(4,
3、"d",76);Insert(head,p);p=Create(5,"e",83);Insert(head,p);Output(head);p=Search(head,2);Delete(head,p);Output(head);return0;}Student*Create(){Student*head;head=(Student*)malloc(sizeof(Student));head->next=NULL;returnhead;}Student*Create(inti,char*ch,floatsc){Student*p;p=(Student*)malloc(sizeof(Studen
4、t));p->id=i;p->name=ch;p->score=sc;p->next=NULL;returnp;}voidInsert(Student*h,Student*p){/*Student*cur;cur=h;while(cur->next!=NULL){cur=cur->next;}cur->next=p;*/if(h->next==NULL){h->next=p;}else{Student*cur,*q;for(q=h->next;q!=NULL;q=q->next){if(q->id>p->id)break;}for(cur=h;cur->next!=q;cur=cur->nex
5、t);if(q==NULL){cur->next=p;}else{p->next=q;cur->next=p;}}}voidOutput(Student*h){if(h->next==NULL){cout<<"链表为空!"<next;while(cur!=NULL){cout<<"t"<id<<"t"<name<<"t"<score<next;}}}Stude
6、nt*Search(Student*h,constintid){if(h->next==NULL){cout<<"链表为空,不能查找!"<next;while(cur!=NULL){if(id==cur->id)break;cur=cur->next;}if(cur==NULL){cout<<"没有找到!"<next==NULL){cout<<"链表为空,不能删除!"<7、dent*cur;cur=h->next;while(cur->next!=p){cur=cur->next;}cur->next=p->next;p=NULL;free(p);}}