第十四周C++【任務二】關於動態鏈表

【關於題目】
  動態鏈表也是程序設計中的一種非常有用的數據結構。可以說,是否能夠理解有關操作的原理,決定了你是否有資格稱爲“科班”出身。在C++程序設計中解決相關問題不免讓人有些害怕,所幸在是,在後續的專業基礎課中,相關的內容還會從不同的角度,反覆地認識,反覆地實踐。不過,在現階段多些體驗,也是很有必要的了。
  先閱讀下面的程序,回顧一下動態鏈表,閱讀程序過程中,請用筆畫一畫形成鏈表的過程中指針值的變化。
  1. #include <iostream>   
  2. using namespace std;  
  3. struct Student  
  4. {   int num;  
  5.     double score;  
  6.     struct Student *next;  
  7. };  
  8. int main( )  
  9. {   Student *head=NULL,*p,*q;  
  10.     //建立動態鏈表  
  11.     for(int i=0;i<3;i++)  
  12.     {  
  13.         p = new Student;  
  14.         cin>>p->num>>p->score;  
  15.         p->next=NULL;  
  16.         if (i==0) head=p;  
  17.         else q->next=p;  
  18.         q=p;  
  19.     }  
  20.     //輸出動態鏈表  
  21.     p=head;  
  22.     while(p!=NULL)  
  23.     {   cout<<p->num<<" "<<p->score<<endl;     
  24.         p=p->next;  
  25.     }  
  26. }  
  可以參考下圖理解程序


【題目】建立專門的鏈表類處理有關動態鏈表的操作
  現在我們通過下面的任務,用面向對象的程序設計的思維看待最簡單的動態鏈表,初步體驗有關的操作。
  任務要求是:在已有代碼的基礎上完善程序,完成動態鏈表的簡單操作。

  1. class Student  
  2. {                            
  3. public:  
  4.     Student(int n,double s){num=n;score=s;next=NULL;}  
  5.     Student *next;  
  6.     int num;  
  7.     double score;  
  8. };  
  9.   
  10. class MyList  
  11. {  
  12. public:  
  13.     MyList(){head=NULL;}  
  14.     MyList(int n,double s){head=new Student(n,s);} //以Student(n,s)作爲單結點的鏈表  
  15.     int display();  //輸出鏈表,返回值爲鏈表中的結點數  
  16.     void insert(int n,double s);  //插入:將Student(n,s)結點插入鏈表,該結點作爲第一個結點  
  17.     void append(int n,double s);  //追加:將Student(n,s)結點插入鏈表,該結點作爲最後一個結點  
  18.     void cat(MyList &il); //將鏈表il連接到當前對象的後面  
  19.     int length();  //返回鏈表中的結點數  
  20. private:  
  21.     Student *head;  
  22. };  
  23.   
  24. int main()  
  25. {  
  26.     int n;  
  27.     double s;  
  28.     MyList head1;  
  29.     cout<<"input head1: "<<endl;  //輸入head1鏈表  
  30.     for(int i=0;i<3;i++)  
  31.     {  
  32.         cin>>n>>s;  
  33.         head1.insert(n,s);  //通過“插入”的方式  
  34.     }  
  35.     cout<<"head1: "<<endl; //輸出head1  
  36.     head1.display();  
  37.   
  38.     MyList head2(1001,98.4);  //建立head2鏈表  
  39.     head2.append(1002,73.5);  //通過“追加”的方式增加結點  
  40.     head2.append(1003,92.8);  
  41.     head2.append(1004,99.7);  
  42.     cout<<"head2: "<<endl;   //輸出head2  
  43.     head2.display();  
  44.   
  45.     head2.cat(head1);   //反head1追加到head2後面  
  46.     cout<<"length of head2 after cat: "<<head2.length()<<endl;  
  47.     cout<<"head2 after cat: "<<endl;   //顯示追加後的結果  
  48.     head2.display();  
  49.   
  50.     system("pause");  
  51.     return 0;  
  52. }  


#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){head=new Student(n,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()  
{  
    if(head==NULL)  
    {  
        cout<<"empty\n";  
        return 0;  
    }  
    int i=0;  
    Student *pt=head;  
    while(pt)  
    {  
        ++i;  
        cout<<pt->num<<" "<<pt->score<<endl;  
        pt=pt->next;  
    }  
    return i;  
}  
  
void MyList::insert(int n, double s)  
{  
    Student * pt=new Student(n,s);  
    pt->next =head;  
    head=pt;  
}  
  
void MyList::append(int n,double s)  
{  
    Student * pt=new Student(n,s);  
    if(head==NULL)  
        head=pt;  
    else   
    {  
        Student *pts=head;  
        Student *pte=pts->next;  
        while(pte)  
        {  
            pts=pte;  
            pte=pts->next;  
        }  
        pts->next=pt;  
    }  
}  
  
void MyList::cat(MyList& il)  
{  
    Student *pt=il.head;  
    while(pt)  
    {  
        append(pt->num,pt->score);  
        pt=pt->next;  
    }  
}  
  
int MyList::length()  //返回鏈表中的結點數  
{  
    int length=0;  
    Student *s=head;//指向鏈表開頭的  
    while(s->next != NULL)    
    {    
        s = s->next;   
        ++length;  
    }   
    return length;  
} 
  
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; //輸出head1  
    head1.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;   //輸出head2  
    head2.display();  
  
    head2.cat(head1);   //反head1追加到head2後面  
    cout<<"length of head2 after cat: "<<head2.length()<<endl;  
    cout<<"head2 after cat: "<<endl;   //顯示追加後的結果  
    head2.display();  
  
    system("pause");  
    return 0;  
}  







老師,head1:  的輸出是倒過來的。

是不是所有的程序都是  先建立的後輸出,還是這是鏈表的特點啊?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章