数据结构实验三单链表学生信息

#include<iostream>
#include<stdlib.h>
using namespace std;
class Student
{private:
struct Node
{char name[20];
char age[4];
int number;
int score;
Node *next;};
Node *top;
public:static int count;
Student(){top=NULL;}
void Insert()  //输入//
{count++;cout<<"第"<<Student::count<<"个学生信息:"<<endl;
Node *s; s=(Node*)malloc(sizeof(Node));
                cout<<"请输入你的名字:";cin>>s->name;
cout<<"请输入你的年龄:";cin>>s->age;
cout<<"请输入你的学号:";cin>>s->number;
cout<<"请输入你的分数:";cin>>s->score;
s->next=top;top=s;
}
void Delete(int a)    //删除//
{if(top==NULL)cout<<"数据已空!"<<endl;else{Node *t;t=top;int n=0;
do{if(t->number!=a)n++;t=t->next;if(n==Student::count){cout<<"找不到该学生!退出程序!";t=NULL;}}while(t!=NULL);}
if(top->number==a){Node *p;p=top;top=top->next;delete p;count--;}
   else {Node *t;t=top;
do{t=t->next;}while(t->number!=a&&t!=NULL);
if(t->number==a){Node *p;p=t;t=t->next;delete p;count--;}
}}
void Search(int b)    //查找//
{if(top==NULL)cout<<"数据已空!"<<endl;else{Node *t;t=top;int n=0;
do{if(t->number!=b)n++;t=t->next;if(n==Student::count){cout<<"找不到该学生!退出程序!";t=NULL;}}while(t!=NULL);}
if(top->number==b)
{cout<<"名字:"<<top->name;
cout<<"年龄:"<<top->age;
cout<<"学号:"<<top->number;
cout<<"分数:"<<top->score<<endl;}
else{Node *t;t=top;do{t=t->next;}while(t->number!=b&&t!=NULL);
if(t->number==b)
{cout<<"名字:"<<t->name;
cout<<"年龄:"<<t->age;
cout<<"学号:"<<t->number;
cout<<"分数:"<<t->score<<endl;}
}  
}
void Show()   //显示//
{if(top==NULL)cout<<"数据已空!"<<endl;else{Node *t;t=top;int n=0;
do{
cout<<"名字:"<<t->name;
    cout<<"年龄:"<<t->age;
cout<<"学号:"<<t->number;
cout<<"分数:"<<t->score<<endl;
t=t->next;n++;if(n==Student::count)t=NULL;}while(t!=NULL);}cout<<endl;}//n控制输出次数,当输出次数等于count时强制停止输出//
};
int Student::count=0;    //静态数据成员赋值//
int main()
{Student S;system("color 02");//改变窗口颜色//
void menu();    //声明菜单//
int i;
do{menu();    //建立一个菜单//
cout<<"请输入你的选择:";cin>>i;
switch(i)
{case 1:S.Insert();break;
case 2:int n;cout<<"请输入你要删除的学生学号:";cin>>n;S.Delete(n);break;
case 3:int m;cout<<"请输入你要查找的学生学号:";cin>>m;S.Search(m);break;
case 4:S.Show();break;
case 5:break;
default:cout<<"选择错误!请重新选择:";break;
}}while(i!=5);
return 0;}
void menu()    //定义菜单//
{cout<<"1--输入;"<<endl;
 cout<<"2--删除;"<<endl;
 cout<<"3--查找;"<<endl;
 cout<<"4--显示;"<<endl;
 cout<<"5--退出."<<endl;}

猜你喜欢

转载自blog.csdn.net/guangnianaaa/article/details/80247155