您的位置:首页 > 其它

单链表实现(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: