第十三周可选项目:链表类

来源:互联网 发布:山东广电网络集团吧 编辑:程序博客网 时间:2024/06/06 10:44

动态链表也是程序设计中的一种非常有用的数据结构。可以说,是否能够理解有关操作的原理,决定了你是否有资格称为“科班”出身。在后续的专业基础课中,相关的内容还会从不同的角度,反复地认识,反复地实践。不过,在现阶段多些体验,也是很有必要的了。
(1)阅读下面的程序,回顾一下动态链表,阅读程序过程中,请用笔画一画形成链表的过程中指针值的变化。

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. using namespace std;  
  3. struct Student  
  4. {  
  5.     int num;  
  6.     double score;  
  7.     struct Student *next;  
  8. };  
  9. int main( )  
  10. {  
  11.     Student *head=NULL,*p,*q;  
  12.     //建立动态链表  
  13.     for(int i=0; i<3; i++)  
  14.     {  
  15.         p = new Student;  
  16.         cin>>p->num>>p->score;  
  17.         p->next=NULL;  
  18.         if (i==0) head=p;  
  19.         else q->next=p;  
  20.         q=p;  
  21.     }  
  22.     //输出动态链表  
  23.     p=head;  
  24.     while(p!=NULL)  
  25.     {  
  26.         cout<<p->num<<" "<<p->score<<endl;  
  27.         p=p->next;  
  28.     }  
  29.     return 0;  
  30. }  

(2)请在下面已有代码的基础上完善程序,完成动态链表的简单操作,程序运行的截图供参考。
[cpp] view plaincopyprint?
  1. class Student  //结点类  
  2. {  
  3. public:  
  4.     Student(int n,double s):num(n), score(s), next(NULL) {}  
  5.     ~Student();  
  6.     Student *next;   //指向下一个结点  
  7.     int num;  
  8.     double score;  
  9. };  
  10.   
  11. class MyList  //链表类,其中的成员是学生   
  12. {  
  13. public:  
  14.     MyList() { head=NULL;     }  
  15.     MyList(int n,double s); //以Student(n,s)作为单结点的链表  
  16.     ~MyList();  
  17.     int display();  //输出链表,返回值为链表中的结点数  
  18.     void insert(int n,double s);  //插入:将Student(n,s)结点插入链表,该结点作为第一个结点  
  19.     void append(int n,double s);  //追加:将Student(n,s)结点插入链表,该结点作为最后一个结点  
  20.     void cat(MyList &il); //将链表il连接到当前对象的后面  
  21.     int length();  //返回链表中的结点数(另一种处理,可以将结点数,作为一个数据成员)  
  22. private:  
  23.     Student *head;   //链表的头结点  
  24. };  
  25. //以下为类成员函数的定义  
  26. ……  
  27. //测试函数  
  28. int main()  
  29. {  
  30.     int n;  
  31.     double s;  
  32.     MyList head1;  
  33.     cout<<"input head1: "<<endl;  //输入head1链表  
  34.     for(int i=0; i<3; i++)  
  35.     {  
  36.         cin>>n>>s;  
  37.         head1.insert(n,s);  //通过“插入”的方式  
  38.     }  
  39.     cout<<"head1: "<<endl; //输出head1  
  40.     head1.display();  
  41.   
  42.     MyList head2(1001,98.4);  //建立head2链表  
  43.     head2.append(1002,73.5);  //通过“追加”的方式增加结点  
  44.     head2.append(1003,92.8);  
  45.     head2.append(1004,99.7);  
  46.     cout<<"head2: "<<endl;   //输出head2  
  47.     head2.display();  
  48.   
  49.     head2.cat(head1);   //把head1追加到head2后面  
  50.     cout<<"length of head2 after cat: "<<head2.length()<<endl;  
  51.     cout<<"head2 after cat: "<<endl;   //显示追加后的结果  
  52.     head2.display();  
  53.     return 0;  
  54. }  

代码:

#include<iostream>using namespace std;class Student  //结点类{public:    Student(int n,double s)    {        num=n;        score=s;        next=NULL;    }    ~Student()    {        if(!next)            delete next;        next=NULL;    };    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;   //链表的头结点};MyList::MyList(int n,double s)//构造函数,以Student(n,s)作为单结点的链表{    head=new Student(n,s);}MyList::~MyList()    //析构函数,有new,就要有delete{    Student *p=head, *q;    while (p != NULL)    {        q = p;        p = p->next;        delete q;    }    head = NULL;    //删除后,把第一个结点的指针设置为空指针}int MyList::display()    //输出链表,返回值为链表中的结点数{    if(head==NULL)     {        cout<<"empty\n";        return 0;    }    int cnt=0;    Student *pt=head;     while(pt)    {        ++cnt;        cout<<pt->num<<", "<<pt->score<<endl;        pt=pt->next;    }    return cnt;}void MyList::insert(int n, double s) //插入:将Student(n,s)结点插入链表,该结点作为第一个结点{    Student * pt=new Student(n,s);    pt->next =head;    head=pt;}void MyList::append(int n,double s) //追加:将Student(n,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) //将链表il连接到当前对象的后面{    Student *pt=il.head;    while(pt)    {        append(pt->num,pt->score);        pt=pt->next;    }}int MyList::length()  //返回链表中的结点数{    int cnt=0;    Student *pt=head;    while(pt)    {        ++cnt;        pt=pt->next ;    }    return cnt;}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;}



0 0
原创粉丝点击