實驗三 間接定址實現學生成績管理系統
阿新 • • 發佈:2018-12-03
#include<iostream> using namespace std; const int M=100; typedef int DataType; struct Node { DataType data; }; class Listlink { public: Listlink(); Listlink(int a[],int n); int Length(){return length;} int Get(int i); int Delete(int i); void Insert(int i,int x); void Print(); private: Node *first[M]; int length; }; Listlink::Listlink() { for(int i=0;i<M;i++) first[i]=NULL; length=0; } Listlink::Listlink(int a[],int n) { for(int i=0;i<M;i++) { first[i]=new Node; first[i]->data=a[i]; } length=n; } int Listlink::Get(int i) { if(i<1||i>length) throw"查詢位置非法"; else return first[i-1]->data; } int Listlink::Delete(int i) { int x,j; if(length==0) throw"下溢"; if(i<1||i>length) throw"位置"; x=first[i-1]->data; for(j=i;j<length;j++) first[j-1]->data=first[j]->data; length--; return x; } void Listlink::Insert(int i,int x) { int j; if(length>=M) throw"上溢"; if(i<1||i>length+1) throw"位置"; for(j=length;j>=i;j--) first[j]->data=first[j-1]->data; first[i-1]->data=x; length++; } void Listlink::Print() { int i; for( i=0;i<length;i++) { cout<<endl<<"第"<<i+1<<"個學生成績"<<first[i]->data<<" "; } } int main() { cout<<"間接定址實現學生成績"<<endl; cout<<endl; int score[3]={77,88,99}; Listlink L(score,3); cout<<"學生的資料結構成績為:"<<endl; L.Print(); cout<<endl<<endl<<"在位置2插入成績66:"<<endl; L.Insert(2,66); L.Print(); cout<<endl<<endl<<"在位置1刪除:"<<endl; L.Delete(1); L.Print(); cout<<endl<<endl<<"位置3的成績為:"<<endl; cout<<L.Get(3)<<endl; cout<<endl; return 0; }