#include <iostream>
using namespace std;
const int N=100;
struct Std{
int date;
int next;
}score[N];
class Student{
private:
static int first;
static int avail;
int length;
public:
Student();
Student(int a[],int n);
~Student(){}
void Insert(int i,int x);
int Delete(int i);
int Get(int i);
int Locate(int x);
void Show();
};
int Student::first=0;
int Student::avail=1;
Student::Student(){
score[first].next=-1;
for (int i=avail;i<N-1;i++)
score[i].next=i+1;
score[i].next=-1;
}
Student::Student(int a[],int n){
int s;cout<<first;
score[first].next=avail;
for (int i=0;i<n;i++){
s=avail;
avail=score[avail].next;
score[s].date=a[i];
score[s].next=avail;
}
length=n;
}
void Student::Insert(int i,int x){
if (i>length) throw "位置非法";
int s;
s=avail;
avail=score[avail].next;
score[s].date=x;
score[s].next=score[i-1].next;
score[i-1].next=s;
length++;
}
int Student::Delete(int i){
if (i>length) throw "位置非法";
int r=first;
while (score[r].next<i)
r=score[r].next;
score[r].next=score[i].next;
score[i].next=avail;
avail=i;
length--;
return score[i].date;
}
int Student::Get(int i) {
if (i>length) throw "位置非法";
int r=first;
while (r<i)
r=score[r].next;
return score[r].date;
}
int Student::Locate(int x) {
int r=first;
while (score[r].date!=x)
r=score[r].next;
return r;
}
void Student::Show() {
int r=first;
for (int j=0;j<length;j++){
r=score[r].next;
cout<<score[r].date<<' ';}
cout<<endl;
}
int main() {
Student Ava;
int a[3] = { 90,80,75 };
Student S(a, 3);
cout << "原表:" << endl;
S.Show();
cout << "在第3位插入98:" << endl;
S.Insert(3, 98);
cout << "插入后:" << endl;
S.Show();
cout << "删除第2位:" << endl;
S.Delete(2);
cout << "删除后:" << endl;
S.Show();
cout << "查75分的位置:" << S.Locate(75) << ',' << "查第2位的成绩:" << S.Get(2) << endl;
return 0;
}
实验三静态链表
最新推荐文章于 2023-02-20 16:42:21 发布