#include<iostream.h>
struct Node
{
int number;
char* name;
char* Class;
float math_grade;
Node *next,*prior;
};
class Student
{
public:
Student();
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:
Node *first;
};
Student::~Student()
{
Node *r,*s;
s=first;
do{r=s;
s=s->next;
delete r;}
while(s==first);
}
Student::Student(char*n[],char*c[],int num[],float m[],int l)
{
Node *s,*r;
first=new Node;
r=first;
for(int i=0;i<l;i++)
{
s=new Node;
s->name=n[i];
s->Class=c[i];
s->number=num[i];
s->math_grade=m[i];
r->next=s;
r=s;
}
r->next=first;
first->prior=r;
}
void Student::Insert(char*n,char*c,int num,float m,int l)
{
Node *r,*s;
int count=0;
r=first;
do
{
r=r->next;
count++;
}
while(r!=first&&count<l-1);
if(r==first) cout<<"位置过大"<<endl;
else
{
s=new Node;
s->name=n;
s->Class=c;
s->number=num;
s->math_grade=m;
s->next=r->next;
s->prior=r;
r->next->prior=s;
r->next=s;
}
}
void Student::Delete(int l)
{ Node *r,*s; int count=0; r=first; do { r=r->next; count++; } while(r!=first&&count<l-1); if(r==first) cout<<"Position is empty" <<endl; else
{
s=r->next;
s->next->prior=r;
r->next=s->next;
cout<<s->name<<"的数据已被删除"<<endl;
delete s;
}
}
void Student::Locate(int l)
{
Node *s;
int count=0;
s=first;
do
{
s=s->next;
count++;
}
while(s!=first&&count<l);
if(s==first) cout<<"位置为空"<<endl;
else
{
cout<<"第"<<l<<"位的数据为:"<<endl;
cout<<"姓名:"<<s->name<<endl;
cout<<"班级"<<s->Class<<endl;
cout<<"学号"<<s->number<<endl;
cout<<"数学成绩"<<s->math_grade<<endl;
cout<<endl;
}
}
void Student::Print()
{
Node *s;
s=first->next;
cout<<"姓名 班级 学号 数学成绩"<<endl;
while(s!=first)
{
cout<<s->name<<" ";
cout<<s->Class<<" ";
cout<<s->number<<" ";
cout<<s->math_grade<<endl;
s=s->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<<"The data entered is:"<<endl;
a.Print();
cout<<endl;
cout<<"Insert data"<<endl;
cout<<endl;
cout<<"Enter the data you want to include name:";
cin>>x;
cout<<"Enter the class you want to be included in:";
cin>>r;
cout<<"Enter the student number you want to be included in:";
cin>>y;
cout <<"Enter the math score you want to include:";
cin>>z;
cout<<"Enter the position you want to include:";
cin>>p;
cout<<endl;
a.Insert(x,r ,y,z,p);
a.Print();
cout<<endl;
cout<<"Position to delete:";
cin>>p;
a.Delete(p);
cout<<endl;
cout<<"Location of the data to be found:";
cin>>p;
a.Locate(p);
cout<<endl;
cout<<"The member data is as follows:"<<endl;
a.Print();
return 0;
struct Node
{
int number;
char* name;
char* Class;
float math_grade;
Node *next,*prior;
};
class Student
{
public:
Student();
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:
Node *first;
};
Student::~Student()
{
Node *r,*s;
s=first;
do{r=s;
s=s->next;
delete r;}
while(s==first);
}
Student::Student(char*n[],char*c[],int num[],float m[],int l)
{
Node *s,*r;
first=new Node;
r=first;
for(int i=0;i<l;i++)
{
s=new Node;
s->name=n[i];
s->Class=c[i];
s->number=num[i];
s->math_grade=m[i];
r->next=s;
r=s;
}
r->next=first;
first->prior=r;
}
void Student::Insert(char*n,char*c,int num,float m,int l)
{
Node *r,*s;
int count=0;
r=first;
do
{
r=r->next;
count++;
}
while(r!=first&&count<l-1);
if(r==first) cout<<"位置过大"<<endl;
else
{
s=new Node;
s->name=n;
s->Class=c;
s->number=num;
s->math_grade=m;
s->next=r->next;
s->prior=r;
r->next->prior=s;
r->next=s;
}
}
void Student::Delete(int l)
{ Node *r,*s; int count=0; r=first; do { r=r->next; count++; } while(r!=first&&count<l-1); if(r==first) cout<<"Position is empty" <<endl; else
{
s=r->next;
s->next->prior=r;
r->next=s->next;
cout<<s->name<<"的数据已被删除"<<endl;
delete s;
}
}
void Student::Locate(int l)
{
Node *s;
int count=0;
s=first;
do
{
s=s->next;
count++;
}
while(s!=first&&count<l);
if(s==first) cout<<"位置为空"<<endl;
else
{
cout<<"第"<<l<<"位的数据为:"<<endl;
cout<<"姓名:"<<s->name<<endl;
cout<<"班级"<<s->Class<<endl;
cout<<"学号"<<s->number<<endl;
cout<<"数学成绩"<<s->math_grade<<endl;
cout<<endl;
}
}
void Student::Print()
{
Node *s;
s=first->next;
cout<<"姓名 班级 学号 数学成绩"<<endl;
while(s!=first)
{
cout<<s->name<<" ";
cout<<s->Class<<" ";
cout<<s->number<<" ";
cout<<s->math_grade<<endl;
s=s->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<<"The data entered is:"<<endl;
a.Print();
cout<<endl;
cout<<"Insert data"<<endl;
cout<<endl;
cout<<"Enter the data you want to include name:";
cin>>x;
cout<<"Enter the class you want to be included in:";
cin>>r;
cout<<"Enter the student number you want to be included in:";
cin>>y;
cout <<"Enter the math score you want to include:";
cin>>z;
cout<<"Enter the position you want to include:";
cin>>p;
cout<<endl;
a.Insert(x,r ,y,z,p);
a.Print();
cout<<endl;
cout<<"Position to delete:";
cin>>p;
a.Delete(p);
cout<<endl;
cout<<"Location of the data to be found:";
cin>>p;
a.Locate(p);
cout<<endl;
cout<<"The member data is as follows:"<<endl;
a.Print();
return 0;
}
operation result: