第十三週項目四 鏈表類

項目要求

動態鏈表也是程序設計中的一種非常有用的數據結構。可以說,是否能夠理解有關操作的原理,決定了你是否有資格稱爲“科班”出身。在後續的專業基礎課中,相關的內容還會從不同的角度,反覆地認識,反覆地實踐。不過,在現階段多些體驗,也是很有必要的了。
(1)閱讀下面的程序,回顧一下動態鏈表,閱讀程序過程中,請用筆畫一畫形成鏈表的過程中指針值的變化。

#include <iostream> 
using namespace std;
struct Student
{	int num;
	double score;
	struct Student *next;
};
int main( )
{	Student *head=NULL,*p,*q;
	//建立動態鏈表
	for(int i=0;i<3;i++)
	{
		p = new Student;
		cin>>p->num>>p->score;
		p->next=NULL;
		if (i==0) head=p;
		else q->next=p;
		q=p;
	}
	//輸出動態鏈表
	p=head;
	while(p!=NULL)
	{	cout<<p->num<<" "<<p->score<<endl;   
		p=p->next;
	}
}

(2)請在下面已有代碼的基礎上完善程序,完成動態鏈表的簡單操作,程序運行的截圖供參考。

class Student  //結點類
{
public:
    Student(int n,double s):num(n), score(s), next(NULL) {}
    ~Student();
    Student *next;   //指向下一個結點
    int num;
    double score;
};

class MyList  //鏈表類,其中的成員是學生 
{
public:
    MyList() { head=NULL;     }
    MyList(int n,double s); //以Student(n,s)作爲單結點的鏈表
    ~MyList();
    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 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();
    return 0;
}

代碼如下

#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 n=0;
    Student *p=head;
    while (p)
    {
        cout<<p->num<<" "<<p->score<<endl;
        p=p->next;
        n++;
    }
    return n;
}

void MyList::insert(int n,double s)//!!!
{
    Student *p;
    p=new Student(n,s);
    p->next=head;
    head=p;
}

void MyList::append(int n,double s)
{
    Student *p;
    p=new Student(n,s);
    if (head==NULL)
        head=p;
    else
    {
        Student *pt=head;
        Student *pts=pt->next;
        while(pts)//!!!!之前寫成while(pt)一直報錯 簡直要哭
        {
            pt=pts;
            pts=pt->next;
        }
        pt->next=p;
    }

}

void MyList::cat(MyList &il)
{
    Student *p=il.head;
    while (p)
    {
        append(p->num,p->score);
        p=p->next;

    }
}

int MyList::length()
{
    int n=0;
    Student *p=head;
    while (p)
    {
        p=p->next;
        n++;
    }
    return n;
}

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();


    return 0;
}
<span style="font-family:Microsoft YaHei;font-size:12px;">
</span>

運行結果



學習心得

感覺沒太懂 爲了找到Bug幾乎是對着賀老的解答進行修正

反覆看了雲學堂第21講視頻…還是不太懂 keep on learning


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