01./* 02.* 程式的著作權和版本聲明部分 03.* Copyright (c)2013, 煙台大學電腦學院學生 04.* All rightsreserved. 05.* 檔案名稱: Student.cpp 06.* 作 者:趙冠哲 07.* 完成日期:2013年6月7日 08.* 版本號碼: v1.0 09.* 輸入描述: 10.* 問題描述: 11.*/ #include<iostream>using namespace std;class Student //結點類{public:Student(int n,double s){num=n;score=s;next=NULL;}Student *next; //指向下一個結點int num;double score;};class MyList //鏈表類{public:MyList(){head=NULL;}MyList(int n,double s); //以Student(n,s)作為單結點的鏈表int display(); //輸出鏈表,傳回值為鏈表中的結點數void insert(int n,double s); //插入:將Student(n,s)結點插入鏈表,該結點作為第一個結點void append(int n,double s); //追加:將Student(n,s)結點插入鏈表,該結點作為最後一個結點void cat(MyList &il); //將鏈表il串連到當前對象的後面int length(); //返回鏈表中的結點數private:Student *head; //鏈表的頭結點};//以下為類成員函數的定義int MyList::display(){ Student *p=head; int count=0; while(p!=NULL) { cout<<p->num<<" "<<p->score<<endl; p=p->next; count++; } return count;}void MyList::insert(int n, double s){ Student *p=new Student(n, s); p->next = head; head = p;}void MyList::append(int n, double s){ Student *p = head, *q; while(p!=NULL) { q=p; p=p->next; } q->next=new Student(n, s);}void MyList::cat(MyList &il){ Student *p=head,*q; while(p!=NULL) { q=p; p=p->next; }}int MyList::length(){ Student *p=head; int count=0; while(p!=NULL) { p=p->next; count++; } return count;}//測試函數int main(){int n;double s;MyList head1;cout<<"input head1: "<<endl; //輸入head1鏈表for(int i=0;i<3;i++){cin>>n>>s;head1.insert(n,s); //通過“插入”的方式}cout<<"head1: "<<endl; //輸出head1head1.display();MyList head2(1001,98.4); //建立head2鏈表head2.append(1002,73.5); //通過“追加”的方式增加結點head2.append(1003,92.8);head2.append(1004,99.7);cout<<"head2: "<<endl; //輸出head2head2.display();head2.cat(head1); //把head1追加到head2後面cout<<"length of head2 after cat: "<<head2.length()<<endl;cout<<"head2 after cat: "<<endl; //顯示追加後的結果head2.display();return 0;}