#include<iostream>
using namespace std;const int M=10;
struct Node
{
int data;
};
class Student
{
public:
Student();
Student(int a[],int n);
~Student();
void Input(int i,int x);
int Delete(int i);
int Locate(int x);
void Print();
private:
Node *data[M];
int length;
};
Student::Student()
{
for(int i=0;i<M;i++)
{data[i]=NULL;}
length=0;
}
Student::Student(int a[],int n)
{
if(n<0||n>M)
throw"参数非法";
for(int i=0;i<M;i++)
{data[i]=NULL;}
for(int j=0;j<n;j++)
{
data[j]=new Node;
data[j]->data=a[j];
}
length=n;
}
Student::~Student()
{
Node *p;
for(int i=0;i<length;i++)
{
p=data[i];
data[i]=NULL;
delete p;
}
}
void Student::Input(int i,int x)
{
if(i<1||i>length+1)
throw"位置非法";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=new Node;
data[i-1]->data=x;
length++;
}
int Student::Delete (int i)
{
if(length==0) throw"下溢";
if(i<1||i>length) throw"位置非法";
Node *p=data[i-1];
int x=p->data;
for(int j=i-1;j<length-1;j++)
data[j]=data[j+1];
delete p;
length--;
data[length]=NULL;
return x;
}
int Student::Locate(int x)
{
for(int i=0;i<length;i++)
if(data[i]->data ==x) return i+1;
return 0;
}
void Student::Print()
{
for(int i=0;i<length;i++)
cout<<"第"<<i+1<<"个学生成绩:"<<data[i]->data <<endl;
cout<<endl;
}
void main()
{
int r[5]={80,90,70,60,50};
Student L(r,5);
cout<<"录入学生信息:"<<endl;
L.Print();
cout<<endl;
cout<<"在第2个位置插入85"<<endl;
L.Input(2,85);
cout<<"插入后学生成绩为:"<<endl;
L.Print();
cout<<endl;
cout<<"成绩为85的学生位置为:"<<endl;
cout<<L.Locate(85)<<endl;
cout<<"删除第一个学生成绩"<<endl;
L.Delete(1);
cout<<"删除后学生成绩为:"<<endl;
L.Print();}