天天看點

建立學生成績表,用順序表、單連結清單、雙連結清單、靜态連結清單、間接尋址實作

順序表

#include<iostream>

using namespace std;

const int max=100;

template<typename A>

class seqlist

{

public:

        seqlist();

        seqlist(Ascore[],int n);

        ~seqlist(){}

        voidinsert(int i,int x);

        voidprint();

        intlocate(int x);

        intDelete(int i);

        intfind(int i);

private:

        intdata[max];

        intlength;

};

template<class A>

void seqlist<A>::insert(int i,int x)

{

        intj;

        if(length>max)throw"上溢";

        if(i<1||i>=max)throw"位置非法";

        for(j=length;j>=i;j--)

                data[j]=data[j-i];

        data[i-1]=x;

        length++;

}

template<class A>

seqlist<A>::Delete(int i)

{

        intp;int j;

        if(length==0)throw"下溢";

        if(i<1||i>=max)throw"位置非法";

        p=data[i-1];

        for(j=i;j<length;j++)

                data[j-1]=data[j];

        length--;

        returnp;

}

template<class A>

seqlist<A>::find(int i)

{

        if(i<1&&i>=length;i++)throw"查找位置非法";

        elsereturn data[i-1];

}

template<class A>

int seqlist<A>::locate(int x)

{

        for(inti=0;i<length;i++)

                if(data[i]==x)

                        returni+1;

                return0;

}

template<class A>

void seqlist<A>::print()

{

        for(inti=0;i<length;i++)

                cout<<data[i]<<"";

}

void main()

{

        floatscore[5]={80,95,100,59.5,60.5};

        seqlist<float>student(score,5);

        cout<<"學生成績:"<<endl;

        student.print();

        try

        {

                student.insert(1,66);

        }

        catch(char*s)

        {

                cout<<s<<endl;

        }

        cout<<"執行插入操作後成績為:"<<endl;

        student.print();

        cout<<"分數為100的位置為:"<<endl;

        cout<<student.locate(100)<<endl;

        cout<<"執行删除第2個分數操作,操作前的分數為:"<<endl;

        student.print();

        try

        {

                student.Delete(2);

        }

        catch(char*s)

        {

                cout<<s<<endl;

        }

        cout<<"删除後的分數為:"<<endl;

        student.print();

}

單連結清單

#include<iostream>  

using namespace std;  

template<class T>  

struct Node  

{  

    T data;  

    Node<T>*next;  

 } ;  

 template<class T>  

 class LinkList  

 {  

    public:  

        LinkList();  

        LinkList(T a[],int n);  

        ~LinkList();  

        int Locate(T x);  

        void Insert(int i,T x);  

        T Delete(int i);  

        void PrintList();  

    private:  

        Node<T>*first;  

 };  

 template<class T>  

 LinkList<T>::LinkList()  

 {  

    first=new Node<T>;  

    first->next=NULL;  

 }  

 template<class T>  

 LinkList<T>::LinkList(T a[],int n)  

 {  

    Node<T>*r,*s;  

    first=new Node<T>;  

     r=first;  

     for(int i=0;i<n;i++)  

     {s=new Node<T>;  

     s->data=a[i];  

     r->next=s;r=s;  

      }   

      r->next=NULL;  

 }  

 template<class T>  

 LinkList<T>::~LinkList()  

 {  

    Node<T>*q=NULL;  

    while(first!=NULL)  

    {  

        q=first;  

        first=first->next;  

        delete q;  

     }  

  }   

template<class T>  

void LinkList<T>::Insert(int i,T x)  

{  

    Node<T>*p=first,*s=NULL;  

    int count=0;  

    while(p!=NULL&&count<i-1)  

    {  

        p=p->next;  

        count++;  

    }  

    if(p==NULL)throw"位置";  

    else{  

        s=new Node<T>;s->data=x;  

        s->next=p->next;p->next=s;  

    }  

 }   

template<class T>  

T LinkList<T>::Delete(int i)  

{  

    Node<T>*p=first,*q=NULL;  

    T x;  

    int count=0;  

    while(p!=NULL&&count<i-1)  

    {  

        p=p->next;  

        count++;  

    }  

    if(p==NULL||p->next==NULL)  

    throw"位置";  

    else{  

        q=p->next;x=q->data;  

        p->next=q->next;  

        delete q;  

        return x;  

    }  

  }    

  template<class T>  

  int LinkList<T>::Locate(T x)  

  {  

    Node<T>*p=first->next;  

    int count=1;  

    while(p!=NULL)  

    {  

        if(p->data==x)return count;  

        p=p->next;  

        count ++;  

      }  

      return 0;  

  }  

 template<class T>  

 void LinkList<T>::PrintList()  

 {  

    Node<T>*p=first->next;  

    while(p!=NULL)  

    {  

        cout<<p->data<<" ";  

        p=p->next;  

     }  

     cout<<endl;  

 }  

 int main()  

 {  

int grade[5]={90,80,70,60,59};

LinkList<int>A(grade,5);  

    cout<<"插入成績前成績資料為:"<<endl;  

    A.PrintList();  

    try  

    {  

        A.Insert(3,75);  

     }  

     catch(char*s)  

     {  

        cout<<s<<endl;  

     }  

     cout<<"插入成績後成績資料為:"<<endl;  

     A.PrintList();  

     cout<<"成績為60的元素位置為:";  

     cout<<A.Locate(60)<<endl;  

     cout<<"删除第一個學生成績前成績資料為:"<<endl;  

     A.PrintList();  

     try  

     {  

        A.Delete(1);  

      }   

      catch(char*s)  

      {  

      cout<<s<<endl;  

      }  

      cout<<"執行删除成績後成績資料為:"<<endl;  

      A.PrintList();   

 }  

繼續閱讀