第13周 【项目 - 链表类】(2)

(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(){
        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){head =new Student(n,s);}
MyList::~MyList(){
    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 *p=new Student(n,s);
    p->next=head;
    head=p;
}
void MyList::append(int n,double s){
    Student *p=new Student(n,s);
    if(head==NULL)head=p;
    else {
        Student *pts=head;
        Student *pte=pts->next;
        while(pte){
            pts=pte;
            pte=pts->next;
        }
        pts->next=p;
    }
}
void MyList::cat(MyList& 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;
}

运行结果:

时间: 2024-08-05 05:19:13

第13周 【项目 - 链表类】(2)的相关文章

13周 项目1 点,圆的关系

#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); prote

13周 项目2 圆的比较

#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); prote

十一周 项目4 类族的设计

#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double x=0,double y=0); void setPoint(double,double); double getx() { return x; } double gety() { return y; } void display(); protected: double x,y; }; class

十一周 项目4 类族的设计 完整版

#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double x=0,double y=0); void setPoint(double,double); double getx() { return x; } double gety() { return y; } void display(); protected: double x,y; }; Point

第十一周 项目四 类族的设计】

项目4 - 类族的设计] 按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积.体积并输出并且完成要求的计算任务: (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试: (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试: (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类

第十一周 项目4 类族的设计(1)

项目4 - 类族的设计] 按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积.体积并输出并且完成要求的计算任务: (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试: (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试: (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类

第13周项目2-纯虚函数形类家庭

写一个程序.定义一个抽象基类Shape,它是从衍生3派生类.Circle(周围).Rectangle(矩形).Triangle(三角).例如,下面的main()性能.划定区域并找到一些几何. int main() { Circle c1(12.6),c2(4.9);//建立Circle类对象c1,c2,參数为圆半径 Rectangle r1(4.5,8.4),r2(5.0,2.5);//建立Rectangle类对象r1,r2,參数为矩形长.宽 Triangle t1(4.5,8.4),t2(3.

第11周 项目四-类族的设计

按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积.体积并输出并且完成要求的计算任务: (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试: (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试: (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),

第13周 项目一-动物这样叫

下面是给出的基类Animal声明和main()函数. class Animal { public: virtual void cry() { cout<<"不知哪种动物,让我如何学叫?"<<endl; } }; int main( ){ Animal *p; p = new Animal(); p->cry(); Mouse m1("Jerry",'m'); p=&m1; p->cry(); Mouse m2("