您的位置:首页 > 其它

第十三周项目 链表之三角形

2015-06-03 18:14 260 查看
/* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作者:刘佳琦
* 完成日期:2015年 6 月 3 日
* 版 本 号:v1.0
*
* 问题描述:请在下面已有代码的基础上完善程序,完成动态链表的简单操作,程序运行的截图供参考。
* 程序输入:
* 程序输出:
*/
#include<iostream>
using namespace std;

class Triangle  //结点类
{
public:
    Triangle(double x=0,double y=0,double z=0):a(x),b(y),c(z) {next=NULL;}
    ~Triangle()
    {
        if(!next)
            delete next;
        next=NULL;
    };
    double a;
    double b;
    double c;
    Triangle *next;   //指向下一个结点

};

class MyList  //链表类
{
public:
    MyList()
    {
        head=NULL;
    }
    MyList(double x,double y,double z); //以Triangle (x,y,z)作为单结点的链表
    ~MyList();
    int display();  //输出链表,返回值为链表中的结点数
    void insert(double x,double y,double z);  //插入:将Triangle (x,y,z)结点插入链表,该结点作为第一个结点
    void append(double x,double y,double z);  //追加:将Triangle (x,y,z)结点插入链表,该结点作为最后一个结点
    void cat(MyList &il); //将链表il连接到当前对象的后面
    int length();  //返回链表中的结点数
private:
    Triangle *head;   //链表的头结点
};

MyList::MyList(double x,double y,double z)
{
    head=new Triangle (x,y,z);
}

MyList::~MyList()
{
    Triangle *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;
    Triangle *pt=head;
    while(pt)
    {
        ++cnt;
        cout<<"第"<<cnt<<"个三角形为:";
        cout<<"("<<pt->a<<","<<pt->b<<","<<pt->c<<")"<<endl;
        pt=pt->next;
    }
    return cnt;
}

void MyList::insert(double x,double y,double z)
{
    Triangle * pt=new Triangle (x,y,z);
    pt->next =head;
    head=pt;
}

void MyList::append(double x,double y,double z)
{
    Triangle * pt=new Triangle (x,y,z);
    if(head==NULL)
        head=pt;
    else
    {
        Triangle *pt1=head;
        Triangle *pt2=pt1->next;
        while(pt2)
        {
            pt1=pt2;
            pt2=pt1->next;
        }
        pt1->next=pt;
    }
}

void MyList::cat(MyList& il)
{
    Triangle *pt=il.head;
    while(pt)
    {
        append(pt->a,pt->b,pt->c);
        pt=pt->next;
    }
}

int MyList::length()
{
    int cnt=0;
    Triangle *pt=head;
    while(pt)
    {
        ++cnt;
        pt=pt->next ;
    }
    return cnt;
}

int main()
{
    double a,b,c;
    MyList head1;
    cout<<"input head1: "<<endl;  //输入head1链表
    for(int i=0; i<5; i++)
    {
        cin>>a>>b>>c;
        head1.insert(a,b,c);  //通过“插入”的方式
    }
    cout<<"head1: "<<endl; //输出head1
    head1.display();

   MyList head2(3,4,5);  //建立head2链表
    head2.append(7,8,9);  //通过“追加”的方式增加结点
    head2.append(11,22,13);
    head2.append(15,11,10);
    head2.append(1,2,3);
    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;
}


运行结果:



学习心得:

这个程序虽然是照着之前的链表类的模板写的,但是由于我的粗心,又忘记了初始化,由于next没有初始化,导致后面运行到pt2=pt1->next的时候,程序出错,直接停止运行!这里的next很容易让人遗忘啊.....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: