資料結構實驗三雙鏈表學生資訊
阿新 • • 發佈: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 *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;}
#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;}