靜態連結串列設計一個學生管理系統
阿新 • • 發佈:2018-12-02
#include<iostream.h>
struct Node
{
int number;
char* name;
char* Class;
float math_grade;
int next;
}Stu[10];
class Student
{
public:
Student(char*n[],char*c[],int num[],float m[],int l);
~Student(){}
void Insert(char*n,char*c,int num,float m,int l);//姓名,班級,學號,成績
void Delete(int l);
void Locate(int l);
void Print();
private:
int avail,first;
};
Student::Student(char*n[],char*c[],int num[],float m[],int l)
{
first=0;
avail=1;
for(int i=0;i<8;i++)
{
Stu[i].next=i+1;
}
for( i=1;i<=l;i++)
{
Stu[i].name=n[i-1];
Stu[i].Class=c[i-1];
Stu[i].number=num[i-1];
Stu[i].math_grade=m[i-1];
}
Stu[i-1].next=-1;
if(l==9) avail=-1;
else avail=l+1;
}
void Student::Insert(char*n,char*c,int num,float m,int l)
{
int x;
if(avail==-1) cout<<"滿資料."<<endl;
else
{
x=avail;
avail=Stu[avail].next;
Stu[x].name=n;
Stu[x].Class=c;
Stu[x].number=num;
Stu[x].math_grade=m;
Stu[x].next=Stu[l-1].next;
Stu[l-1].next=x;
if(avail+1==11) avail=-1;
}
}
void Student::Delete(int l)
{
int x,y;
if(l==1)
{
x=y=Stu[first].next;
Stu[first].next=Stu[x].next;
Stu[x].next=avail;
avail=x;
for(int i=0;i<l-1;i++)
y=Stu[y].next;
cout<<Stu[y].name<<"的資料已被刪除."<<endl;
}
else
{
x=y=Stu[first].next;
for(int i=0;i<l-2;i++)
x=Stu[x].next;
for( i=0;i<l-1;i++)
y=Stu[y].next;
cout<<Stu[y].name<<"的資料已被刪除."<<endl;
Stu[x].next=Stu[y].next;
Stu[y].next=avail;
avail=y;
}
}
void Student::Locate(int l)
{
int x;
x=Stu[first].next;
for(int i=0;i<l-1;i++)
x=Stu[x].next;
if(Stu[x].name==NULL) cout<<"這個位置為空"<<endl;
else
{
cout<<"第"<<l<<"位的資料為:"<<endl;
cout<<"姓名:"<<Stu[x].name<<endl;
cout<<"班級"<<Stu[x].Class<<endl;
cout<<"學號"<<Stu[x].number<<endl;
cout<<"數學成績"<<Stu[x].math_grade<<endl;
cout<<endl;
}
}
void Student::Print()
{
int x;
x=Stu[first].next;
cout<<"姓名 班級 學號 數學成績"<<endl;
while(Stu[x].next!=-1)
{
cout<<Stu[x].name<<" ";
cout<<Stu[x].Class<<" ";
cout<<Stu[x].number<<" ";
cout<<Stu[x].math_grade<<endl;
x=Stu[x].next;
}
}
int main()
{
int p;
int y;
float z;
char x[5];
char r[10];
char*n[4]={"小明","小紅","小花","小青"};
char*c[4]={"信管1171","信管1172","計科1171","信管1161"};
int num[4]={2011,2012,2014,2017};
float m[4]={80,90,100,89.5};
Student a(n,c,num,m,4);
cout<<"錄入的資料為:"<<endl;
a.Print();
cout<<endl;
cout<<"插入資料"<<endl;
cout<<endl;
cout<<"輸入你要列入的姓名:";
cin>>x;
cout<<"輸入你要列入的班級:";
cin>>r;
cout<<"輸入你要列入的學號:";
cin>>y;
cout<<"輸入你要列入的數學成績:";
cin>>z;
cout<<"輸入你要列入的位置:";
cin>>p;
cout<<endl;
a.Insert(x,r,y,z,p);
a.Print();
cout<<endl;
cout<<"要刪除的位置:";
cin>>p;
a.Delete(p);
cout<<endl;
cout<<"要查詢的資料的位置:";
cin>>p;
a.Locate(p);
cout<<endl;
cout<<"各成員資料如下:"<<endl;
a.Print();
return 0;
struct Node
{
int number;
char* name;
char* Class;
float math_grade;
int next;
}Stu[10];
class Student
{
public:
Student(char*n[],char*c[],int num[],float m[],int l);
~Student(){}
void Insert(char*n,char*c,int num,float m,int l);//姓名,班級,學號,成績
void Delete(int l);
void Print();
private:
int avail,first;
};
Student::Student(char*n[],char*c[],int num[],float m[],int l)
{
first=0;
avail=1;
for(int i=0;i<8;i++)
{
Stu[i].next=i+1;
}
for( i=1;i<=l;i++)
{
Stu[i].name=n[i-1];
Stu[i].Class=c[i-1];
Stu[i].math_grade=m[i-1];
}
Stu[i-1].next=-1;
if(l==9) avail=-1;
else avail=l+1;
}
void Student::Insert(char*n,char*c,int num,float m,int l)
{
int x;
if(avail==-1) cout<<"滿資料."<<endl;
else
x=avail;
avail=Stu[avail].next;
Stu[x].name=n;
Stu[x].Class=c;
Stu[x].number=num;
Stu[x].math_grade=m;
Stu[x].next=Stu[l-1].next;
Stu[l-1].next=x;
if(avail+1==11) avail=-1;
}
}
void Student::Delete(int l)
{
int x,y;
if(l==1)
{
x=y=Stu[first].next;
Stu[first].next=Stu[x].next;
Stu[x].next=avail;
avail=x;
for(int i=0;i<l-1;i++)
y=Stu[y].next;
cout<<Stu[y].name<<"的資料已被刪除."<<endl;
}
else
{
x=y=Stu[first].next;
for(int i=0;i<l-2;i++)
x=Stu[x].next;
for( i=0;i<l-1;i++)
y=Stu[y].next;
cout<<Stu[y].name<<"的資料已被刪除."<<endl;
Stu[x].next=Stu[y].next;
Stu[y].next=avail;
avail=y;
}
}
void Student::Locate(int l)
{
int x;
x=Stu[first].next;
for(int i=0;i<l-1;i++)
x=Stu[x].next;
if(Stu[x].name==NULL) cout<<"這個位置為空"<<endl;
else
{
cout<<"第"<<l<<"位的資料為:"<<endl;
cout<<"姓名:"<<Stu[x].name<<endl;
cout<<"班級"<<Stu[x].Class<<endl;
cout<<"學號"<<Stu[x].number<<endl;
cout<<"數學成績"<<Stu[x].math_grade<<endl;
cout<<endl;
}
}
void Student::Print()
{
int x;
x=Stu[first].next;
cout<<"姓名 班級 學號 數學成績"<<endl;
while(Stu[x].next!=-1)
{
cout<<Stu[x].name<<" ";
cout<<Stu[x].Class<<" ";
cout<<Stu[x].number<<" ";
cout<<Stu[x].math_grade<<endl;
x=Stu[x].next;
}
}
int main()
{
int p;
int y;
float z;
char x[5];
char r[10];
char*n[4]={"小明","小紅","小花","小青"};
char*c[4]={"信管1171","信管1172","計科1171","信管1161"};
int num[4]={2011,2012,2014,2017};
float m[4]={80,90,100,89.5};
Student a(n,c,num,m,4);
cout<<"錄入的資料為:"<<endl;
a.Print();
cout<<endl;
cout<<"插入資料"<<endl;
cout<<endl;
cout<<"輸入你要列入的姓名:";
cin>>x;
cout<<"輸入你要列入的班級:";
cin>>r;
cout<<"輸入你要列入的學號:";
cin>>y;
cout<<"輸入你要列入的數學成績:";
cin>>z;
cout<<"輸入你要列入的位置:";
cin>>p;
cout<<endl;
a.Insert(x,r,y,z,p);
a.Print();
cout<<endl;
cout<<"要刪除的位置:";
cin>>p;
a.Delete(p);
cout<<endl;
cout<<"要查詢的資料的位置:";
cin>>p;
a.Locate(p);
cout<<endl;
cout<<"各成員資料如下:"<<endl;
a.Print();
return 0;
}
執行結果: