///////////////////////////////////////////////////////////
//// 单链表结构测试程序
///////////////////////////////////////////////////////////
#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);
} */
本文地址:http://com.8s8s.com/it/it26685.htm