单链表结构测试程序(Linktest.cpp)
来源:互联网 发布:阿里云服务器磁盘分区 编辑:程序博客网 时间:2024/05/01 12:13
///////////////////////////////////////////////////////////
//// 单链表结构测试程序
///////////////////////////////////////////////////////////
#include <iostream.h>
#include <fstream.h>
#include "link.h"
#include "complex.h"
void main()
{
//Node类测试代码
Node<int> a(10);
Node<int> b(20, &a);
cout << b.NextNode()->GetData() << endl;
Node<int> *pNode = new Node<int>(33, &b);
cout << pNode->NextNode()->NextNode()->GetData() << endl;
delete pNode;
//LinkedList类测试代码
LinkedList<int> L1; //声明结点数据域为int类型的链表
LinkedList<Complex> L2; //声明结点数据域为Complex类型的链表
register int i;
for (i = 0; i < 10; i++) {
L1.InsertFront(i); //表头插入
L2.InsertRear(Complex(i, i*1.5)); //表尾插入
}
L1.PrintList(); //输出L1(到屏幕)
L1.Reset(5); //定位至位序为5的结点
L1.DeleteAt(); //删除当前位置的结点
L1.DeleteAt(); //删除当前位置的结点
L1.PrintList(); //输出L1
L2.PrintList(" ==> ", 5); //输出L2,以逗号间隔,每行5个
for (i=0; i < 100; i++)
L2.DeleteAtPos(5); //删除位序为5的结点
L2.PrintList(); //输出L2
ofstream of("test.txt"); //新建文件流
L1.PrintList("/t", 2, of); //输出L1到文件,以Tab(/t)间隔,每行2个
}
/* 附:复数类Complex定义如下:
///////////////////////////////////////////////////////////////////
//// Complex.h
///////////////////////////////////////////////////////////////////
#include <iostream.h>
class Complex { //a + bi
private:
double a, b;
public:
Complex(double aa=0, double bb=0) { //带缺省值的构造函数
a = aa;
b = bb;
}
Complex(const Complex &x) { //构造函数的重载(拷贝构造函数)
a = x.a;
b = x.b;
}
double Real() { return a; } //实部
double Image() { return b; } //虚部
double r() { return a; } //取实部
double i() { return b; } //取虚部
void SetRealPart(double aa) { a = aa; } //设置实部
void SetImagePart(double bb) { b = bb; } //设置虚部
//......
Complex operator+(const Complex &x) const {
return Complex(a + x.a, b + x.b);
}
Complex operator+(double x) const {
return Complex(a + x, b);
}
Complex operator-(const Complex &x) const {
return Complex(a - x.a, b - x.b);
}
//其它运算符的重载(略)
friend Complex operator+(double d, const Complex &x);
friend istream& operator >> (istream &stream, Complex &x);
friend ostream& operator << (ostream &stream, const Complex &x);
friend int operator==(const Complex &x, const Complex &y);
};
istream& operator >> (istream &stream, Complex &x) {
return stream >> x.a >> x.b;
}
ostream& operator << (ostream &stream, const Complex &x) {
stream << x.a;
if (x.b > 0)
stream << '+' << x.b << 'i';
else if (x.b < 0)
stream << x.b << 'i';
return stream;
}
int operator==(const Complex &x, const Complex &y) {
return (x.a == y.a) && (x.b == y.b);
}
Complex operator+(double d, const Complex &x) {
return Complex(d + x.a, x.b);
} */
- 单链表结构测试程序(Linktest.cpp)
- 初步的测试程序--main.cpp
- 结构体对齐测试测试程序
- 用LUA扩展cpp程序的几个测试
- thrift cpp 相关测试
- 算法竞赛入门经典 6.2.2键式结构 程序源码list2.cpp错误
- Cpp Concurrency In Action(读书笔记9)——多线程程序的测试和调试
- 第一个cpp程序
- cpp程序性能优化
- cpp程序性能优化
- 几个cpp程序
- cpp-DS:例子程序
- [C] 结构体测试程序 -- 学生信息管理系统
- Curator源码解析(一)源码结构和测试程序
- cpp 进制转换 测试
- ndroid中的自动测试这里结合Music的测试程序,分析一下Android测试程序的结构和执行流程。
- 最简单的Cpp程序
- cocos2dx cpp程序打印方法
- asp页面在VS.NET中无法打开的问题解决(转)
- Java 5.0 内置了JMX
- 今天上了转到计算机专业来的第一节跟专业有关的课
- 今天获得1GB的Google Gmail邮箱一个
- Editplus v2.12(139)
- 单链表结构测试程序(Linktest.cpp)
- Tomcat on Linux
- 在.NET中调用Oracle9i存储过程经验总结
- 急求一个用C#写的日历!
- 难得加班
- JAVA编写的日历
- 请教:在java中如何实现线程的函数互斥调用,用synchronized好像实现不了
- sql有关日期的实现
- IPMP讲座