实验三顺序表

#include <iostream>    
using namespace std;    
const int N=5;    
class Student{    
private:    
    int scores[N];    
    int length;    
public:    
    Student(int score[],int n);    
    ~Student(){}    
    void Insert(int i,int x);    
    int Delete(int i);    
    int Get(int i);    
    int Locate(int x);    
    void Show();    
};    
Student::Student(int score[],int n){    
    if (n>N) throw "参数非法";    
    for (int i=0;i<n;i++)    
        scores[i]=score[i];    
    length=n;    
}    
void Student::Insert(int i,int x){    
    if (lenght>N) throw "上溢";    
    if (i<0||i>length+1) throw "位置非法";    
    for (int j=length;j>=i;j--)    
        scores[j]=scores[j-1];    
    scores[i-1]=x;    
    length++;    
}    
int Student::Delete(int i){    
    if (length<0) throw "下溢";    
    if (i<0||i>length+1) throw "位置非法";    
    int x=scores[i-1];    
    for (int j=i-1;j<length-1;j++)    
        scores[j]=scores[j+1];    
    length--;    
    return x;    
}    
int Student::Get(int i){    
    return scores[i-1];    
}    
int Student::Locate(int x){    
    int n;    
    for (int i=0;i<length;i++)    
        if (scores[i]==x)    
            n=i+1;    
        return n;    
}    
void Student::Show(){    
    for (int i=0;i<length;i++)    
        cout<<scores[i]<<' ';    
    cout<<endl;    
}    
    
int main (){    
    int a[3]={70,80,95};    
    Student S(a,3);    
    cout<<"原表:"<<endl;    
    S.Show();    
    cout<<"在第3位插入99:"<<endl;    
    S.Insert(3,99);    
    cout<<"插入后:"<<endl;    
    S.Show();    
    cout<<"删除第2位:"<<endl;    
    S.Delete(2);    
    cout<<"删除后:"<<endl;    
    S.Show();    
cout<<"查80分的位置:"<<S.Locate(80)<<','<<"查第2位的成绩:"<<S.Get(2)<<endl;    
    return 0;    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值