1. 程式人生 > >學生成績——間接定址

學生成績——間接定址

#include<iostream>  
using namespace std;  
const int Maxsize=10;  
template<class student>  
struct Node {  
    student data;  
    Node<student> *next;  
};  
  
template<class student>  
class inadd{  
    public:  
        inadd();//無參建構函式  
        inadd(student score[],int n);//有參建構函式  
        ~inadd();//解構函式  
        void print();//遍歷操作  
        student get(int i);//按位查詢操作  
        int Locate(student x);//按值查詢操作  
        void insert(int i,student x);//插入操作  
        student Delete(int i);//刪除操作  
        bool changeList(int i,student x);  //改變某一結點的值 i為節點的位置,x為替換的值  
    private:  
        Node<student> *first; //頭指標  
        int length; //結點數量  
        Node<student> *address[Maxsize];  //結點指標陣列  
};  
  
template<class student>  
inadd<student>::inadd()  
{  
    first=new Node<student>;  
    first->next=NULL;  
}  
  
template<class student>  
inadd<student>::inadd(student score[],int n)  
{  
    if (n>Maxsize) throw("溢位");  
    Node<student> *s;  
    first=new Node<student>;
	first->next=NULL; //初始化一個空連結串列    
    for(int i=n-1;i>=0;i--)    
    {    
        s=new Node<student>;
		s->data=score[i];  //為每個陣列元素建立一個結點    
        s->next=first->next;
		first->next=s;  //將結點s插入頭結點之後    
    }  
}  
  
template<class student>  
inadd<student>::~inadd()                 //解構函式    
{  
    Node<student> *q;  
    while(first!=NULL)  
    {  
        q=first;  
        first=first->next;  
        delete q;  
    }  
}    
  
template<class student>      
void inadd<student>::insert(int i,student x)    
{    
    Node<student>*p,*s;int count;    
    p=first;count=0;    
    while(p!=NULL&&count<i-1)    
    {    
        p=p->next;    
        count++;    
    }    
    if(p==NULL)throw"位置非法";  
    s=new Node<student>;
	s->data=x;  
    s->next=p->next;  
    p->next=s;  
    length++;  
}  
  
template<class student>      
student inadd<student>::Delete(int i)  
{  
    Node<student> *q,*p; 
	student x; 
	int count;    
    p=first;count=0; //注意P指標要指向頭結點    
    while(p!=NULL&&count<i-1)   //此操作目的是找到i-1個結點  
    {  
        p=p->next;  
        count++;    
    }  
    if(p==NULL||p->next==NULL)throw"位置";  //結點p不存在或p後繼結點不存在  
    else{  
        q=p->next;
		x=q->data;  //暫存被刪結點    
        p->next=q->next;    
        delete q;    
        return x;  
    }  
}    
    
template<class student>      
student inadd<student>::get(int i)    
{  
    Node<student>*p;int count;  
    p=first->next;count=1;  
    while(p!=NULL&&count<i)  
    {p=p->next;count++;}  
    if(p==NULL)throw"位置非法";  
    else return p->data;  
}    
    
template<class student>      
int inadd<student>::Locate(student x)    
        {    
            Node<student>*p;
			int count =1;    
            p=first->next;    
            while(p!=NULL)    
            {    
                if(p->data==x)return count;    
                p=p->next;    
                count++;    
            }    
            return 0;    
        }    
    
template<class student>      
void inadd<student>::print()    
        {    
            Node<student>*p;    
            p=first->next;    
            while(p!=NULL)    
            {cout<<p->data<<"  ";    
            p=p->next;    
            }    
        }  
int main()    
{    
    float score[5]={88.5,52.5,99,73.5,98};    
    inadd<float>Student(score,5);    //建立物件    
    cout<<"初始資料如下:"<<endl;      
    Student.print();      
    cout<<endl<<"在位置5插入成績95,結果如下:"<<endl;      
    Student.insert(5,95);      
    Student.print();      
    cout<<endl<<"在學生2刪除成績為:"<<Student.Delete(2)<<endl<<"刪除後結果如下:"<<endl;      
    Student.print();      
    cout<<endl<<"學生2的成績為:"<<Student.get(3)<<endl;      
    cout<<endl<<"成績99所在位置為:"<<Student.Locate(99)<<endl;  
	cout<<"最終資料如下:"<<endl;
	Student.print();
	cout<<endl;
	return 0;
}