单链表实现(student);
2015-10-14 11:17
330 查看
//student.h; #include<iostream> #include<string> using namespace std; class student { public: string name; string num; double score; student* next; }; class Linklist { student* first; public: Linklist(); void SetLinklist();// void printlist(); void seach(); void insert(); void delect(); void change(); }; //....................................................... //studentshixian.cpp; #include"student.h" void Linklist::SetLinklist() { first=new student(); first->next=NULL; int s; int count=0; student* p1,* p; p=first; do { p1=new student(); cout<<"请输入学生的姓名,学号,成绩:"<<endl; cin>>p1->name>>p1->num>>p1->score; p->next=p1; p=p1; cout<<"是否要继续插入,是的话输入1,否则输入0"<<endl; cin>>(int)s; }while(s==1); p->next=NULL;//important return ; }// void Linklist::printlist() { student* p=first->next; if(p==NULL) throw "空指针"; while(p!=NULL) { cout<<"name:"<<p->name<<" "<<"num:"<<p->num<<" "<<"score:"<<p->score<<endl; p=p->next; } cout<<endl; }// void Linklist::seach() { student* p=first->next; int s=1; if(p==NULL) throw "空指针"; string name1; do { cout<<"输请入你要找的学生姓名:"<<endl; cin>>name1; p=first->next;//很重要啊! while(p!=NULL) { if(p->name==name1) { cout<<"name:"<<p->name<<" "<<"num:"<<p->num<<" "<<"score:"<<p->score<<endl; break; } p=p->next; } if(p->name!=name1) cout<<"没有找到这位学生"; cout<<"是否要继续查找,是的话输入1,否则输入0"<<endl; cin>>s; }while(s==1); } void Linklist::insert() { student* p,*p1; p1=first; if(p1->next==NULL) throw "空指针"; int s; do{ cout<<"请输入你要插入学生的姓名,学号,成绩"<<endl; p=new student(); cin>>p->name>>p->num>>p->score; while(p1->next!=NULL) {p1=p1->next;} p1->next=p; p->next=NULL; cout<<"是否要继续插入? 是的话输入1 否则输入0或者其他数字"<<endl; cin>>s; }while(s==1); } // void Linklist::delect() { string name1; int s; student* p1=first; do{ student* p=first->next; if(first->next==NULL) throw "空指针"; cout<<"请输入你要删除学生的姓名:"<<endl; cin>>name1; while(p!=NULL) { if(p->name==name1) break; p1=p; p=p->next; } if(p->name!=name1) cout<< "没有这个学生"; else{ p1->next=p->next; delete p; } cout<<"是否要继续删除,是的话输入1,否则输入0或者其他数字"<<endl; cin>>(int)s; }while(s==1); } void Linklist::change() { string name1; student* p=first->next; int s; if(p==NULL) throw "空指针"; do { p=first->next; cout<<"请输入你要修改的学生姓名:"<<endl; cin>>name1; while(p!=NULL) { if(p->name==name1) break; p=p->next; } if(p==NULL) cout<<"没有找到这位学生"; else {cout<<"请重新输入改学生的姓名,学号,成绩:"<<endl; cin>>p->name;cin>>p->num;cin>>p->score;} cout<<"修改成功!"<<endl; cout<<"是否要继续修改,是的话输入1,否则输入0"<<endl; cin>>s; }while(s==1); } Linklist::Linklist() { first=new student(); first->next=NULL; } //*************************** //maintest.cpp; #include"student.h" int main() { int select; Linklist L; while(1) { system("cls"); cout<< "\t ******************欢迎使用******************\n"; cout<< "\t **************职工信息管理系统**************\n"; cout<< "\t *------------------------------------------*\n"; cout<< "\t * 1——录入职工信息 *\n"; cout<< "\t * 2——显示职工信息 *\n"; cout<< "\t * 3——查询职工信息 *\n"; cout<< "\t * 4——修改职工信息 *\n"; cout<< "\t * 5——添加职工信息 *\n"; cout<< "\t * 6——删除职工信息 *\n"; cout<< "\t * 0——退出 *\n"; cout<< "\t *------------------------------------------*\n"; cout<< "\t 你要输入的编号是(0--6):"; cin>>select; if(select==0) break; switch(select){ case 1: try {L.SetLinklist();} catch(char a[]) { cout<<a<<endl; } system("pause"); break; case 2: try {L.printlist();} catch(char a[]) { cout<<a<<endl; } system("pause"); break; case 3: try { L.seach();} catch(char a[]) { cout<<a<<endl; } system("pause"); break; case 4: try {L.change();} catch(char a[]) { cout<<a<<endl; } system("pause"); break; case 5: try {L.insert();} catch(char a[]) { cout<<a<<endl; } system("pause"); break; case 6: try {L.delect();} catch(char a[]) { cout<<a<<endl; } system("pause"); break; default: cout<<"没有此选项,请重新选择!"<<endl; } } system("pause"); return 0; }
相关文章推荐
- 安装sipp测试工具
- UITableView详谈
- ETC1压缩纹理格式详解
- 练习:输出整数每一位,计算算数,9出现次数,输出图案,水仙花数
- accelio介绍和信息汇总
- 中小手机厂商遇倒闭潮:沦为小米华为炮灰
- JavaScript属性
- Android:下载网络图片
- nodejs API 之典型概念
- 用node-webkit把web应用打包成桌面应用
- Storm(五)Storm on YARN介绍
- 将文章分享到QQ空间的代码
- MySQL学习笔记6:索引
- 文件管理---摘自培训文档
- Gson用户指南(Gson User Guide)
- OpenGL资源
- Theme.AppCompat.Light无法找到问题
- IOS开发学习记录----计算机相关知识
- iOS开发UI篇—iPad和iPhone开发的比较
- RMAN备份与恢复之DataBase