单链表结构测试程序(Linktest.cpp)

///
单链表结构测试程序
///
#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);
} */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值