第十三周项目 链表之三角形
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很容易让人遗忘啊.....
相关文章推荐
- 1.2. chromium源代码分析 - chromiumframe - 入口函数
- Little-endian的一个好处:在变量指针转换的时候地址保持不变
- PhoneGap 3 在 Mac 上安装使用
- Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot evaluate com.hotel.Object_$$_jvst485_15.toString()
- 平面设计新手应该掌握哪些知识
- 适配 - Xcode6中自动布局autolayout和sizeclass的使用
- JS-精确四则运算
- jsp下显示硬盘下绝对路径下的中文文件名的图片
- 诸葛:分析师是如何写好数据研究报告的?
- 移动互联网长连接方案实例(转)
- 类型转换
- 使用ProcDump在程序没有响应时自动收集dump
- A. Tavas and Nafas
- MFC中控件的TAB顺序
- Bluemix云平台实践:构建可穿戴应用(1)
- Android中的volley_5_字节数组缓冲池ByteArrayPool 和使用缓冲池技术的自定义的PoolingByteArrayOutputStream
- (5)mysql优化之MyISAM表锁
- Java 获取计算机参数[IPvsMAC]
- 系统架构设计理论与原则、负载均衡及高可用系统设计速记
- appdata文件夹有什么用途?C盘appdata可以删除吗?