資料結構實驗三單鏈表學生資訊
阿新 • • 發佈:2018-11-21
#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;}
#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;
cout<<"請輸入你的名字:";cin>>s->name;
cout<<"請輸入你的年齡:";cin>>s->age;
cout<<"請輸入你的學號:";cin>>s->number;
cout<<"請輸入你的分數:";cin>>s->score;
}
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;
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;}