1. 程式人生 > >靜態連結串列建立學生成績

靜態連結串列建立學生成績

#include <iostream>
using namespace std;  
const int M=100;  
struct SNode  
{  
        int data;  
        int next;  
}SList[M];
class Student        
{  
    public:  
        Student();  
        Student(int  a[],int n);  
        ~Student();  
        int Length();               //表長   
        int Locate(int x);              //按值查詢   
        void Input(int i,int x);    //插入   
        int Delete(int i);           //刪除   
        void Print();          //遍歷操作   
    private:  
        int first,avail; 
};  
Student::Student()  
{  
    for(int i=0;i<M-1;i++)  
    SList[i].next=i+1;  //結點的next域指向下一節點
    SList[M-1].next=0;  //目前靜態連結串列為空,最後一個元素的next為0;
}  
    Student::Student(int a[],int n)  
{  
    if(n<0||n>M)  
        throw"輸入錯誤";  
    for(int i=0;i<M;i++)  
    {  
        SList[i].next=i+1;  
    }  
    SList[M-1].next=-1;  
    first=0;  
    avail=1;  
    SList[first].next=-1;  
    for(int j=0;j<n;j++)  
    {  
        int s=avail;  //空閒鏈的第一個結點
        avail=SList[avail].next;  //空閒鏈的頭指標後移
        SList[s].data=a[j];  //將陣列填入下標為s的結點
        SList[s].next=SList[first].next;  //將下標為s的結點插入到下一個結點
        SList[first].next=s;  
    }  
}  
 Student::~Student()
{}
    int Student::Length()  
{  
    if(SList[1].next==0)  
        return 0;  
    int i=1;  
    int count=-1;  
    while(i!=0)
    {  
        count++;  
        i=SList[i].next;  
    }  
    return count;  
}  
int Student::Locate(int x)
{   if(SList[first].next==-1)  
        throw"成績為空";  
    int p=first,count=0;  
    while(SList[p].next!=-1)  
    {  
        if(SList[p].data==x) return count;  
        p=SList[p].next;  
        count++;  
    }  
    
    return 0;  
}  


void Student::Input(int i,int x)
{  
    if(i<0||i>M)   
        throw"引數非法";  
    int s=avail;  
    int p=first;  
    if(p==-1)  
        throw"引數非法";  //空鏈
    for(int count=0;count<i-1;count++)  
    {  
        p=SList[p].next;  
    }  
    avail=SList[avail].next;  //空閒鏈的頭指標後移  
    SList[s].data=x;      //將x填入下標為s的結點
    SList[s].next=SList[p].next;  //將下標為s的結點插到下標為p的結點後面
    SList[p].next=s;  
}  
int  Student::Delete(int i)
{if(i<0||i>M)   
        throw"引數非法";  
    int p=first;  
    if(p==-1)  
        throw"引數非法";//空鏈  
    for(int count=0;count<i-1;count++)  
    {  
        p=SList[p].next;  
    }  
    int q=SList[p].next;  //暫存被刪結點的下標
     SList[p].next=SList[q].next;  //摘鏈
    SList[q].next=avail;  //將結點q插在空閒鏈avail的最前端
    avail=q;  //空閒鏈頭指標avail指向結點q
    return 0;  
}  


void Student::Print()
{int p=SList[first].next;  
    while(p!=-1)  
    {     cout<<"第"<<p<<"個學生成績:"<<SList[p].data<<endl;
  
        p=SList[p].next;  
    }  
    cout<<endl;  }
void main()
{int i;
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;