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

#include<iostream>
#include<stdlib.h>
using namespace std;
class Student
{private:
struct Node
{char name[20];
char age[4];
int number;
int score;
Node *piror;
Node *next;};  //建立结点信息//
Node *first;Node *top;
public:static int count;     
   Student(){first=new Node;first->piror=NULL;first->next=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->piror=first;first->next=s;first=s;
}
void Delete(int a){if(first==NULL)cout<<"数据已空!"<<endl;else{Node *t;t=first;int n=0;
do{if(t->number!=a)n++;t=t->piror;if(n==Student::count){cout<<"找不到该学生!退出程序!"<<endl;t->piror=NULL;}}while(t->piror!=NULL);}
if(first->number==a){Node *p;p=first;first=first->piror;delete p;count--;}
   else {Node *t;t=first;
do{t=t->piror;}while(t->number!=a&&t!=NULL);
if(t->number==a){Node *p;p=t;p->next=p->piror;p->piror=p->next;delete p;count--;}
}}    //删除//
void Search(int b){if(first==NULL)cout<<"数据已空!"<<endl;else{Node *t;t=first;int n=0;
do{if(t->number!=b)n++;t=t->piror;if(n==Student::count){cout<<"找不到该学生!退出程序!";t->piror=NULL;}}while(t->piror!=NULL);}
if(first->number==b)
{cout<<"名字:"<<first->name;
cout<<"年龄:"<<first->age;
cout<<"学号:"<<first->number;
cout<<"分数:"<<first->score<<endl;}
else{Node *t;t=first;do{t=t->piror;}while(t->number!=b&&t->piror!=NULL);
if(t->number==b)
{cout<<"名字:"<<t->name;
cout<<"年龄:"<<t->age;
cout<<"学号:"<<t->number;
cout<<"分数:"<<t->score<<endl;}
}}     //查找//
void Show1() {if(Student::count==0)cout<<"数据已空!"<<endl;else{Node *t;t=first;int n=0;//前序显示//
do{
cout<<"名字:"<<t->name;
    cout<<"年龄:"<<t->age;
cout<<"学号:"<<t->number;
cout<<"分数:"<<t->score<<endl;
t=t->piror;n++;if(n==Student::count)t->piror=NULL;}while(t->piror!=NULL);}cout<<endl;}
//void Show2()后序显示//


};
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.Show1();break;
//case 5:S.Show2();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;//
 cout<<"5--退出."<<endl;}

猜你喜欢

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