箱子排序

先上一幅图片

这里写图片描述

这幅图就概括了箱子排序的大致过程。

在箱子排序的过程中,使用了 Chain<T> 类模板、 ChainNode<T> 类模板以及 Node 类,完成箱子排序功能的是 BinSort 成员函数,它是 Chain<T> 的成员。

以下代码已经通过编译,并能成功运行,编译环境是VS2015。

Chain<T> 类模板和 ChainNode<T> 类模板,其中有些功能、注释不是本主题必需的。

#pragma once
#include <iostream>
using namespace std;

template <class T> class Chain;  //加上这句代码,少了很多类似“语法错误:<”的错误
                                 //这可能是因为ChainNode要引用Chain,而Chain必须先声明

template <class T>
class ChainNode
{
    friend class Chain<T>;
private:
    T data;
    ChainNode<T> *link;
};

template<class T> class ChainIterator;

template<class T>
class Chain
{
    friend class ChainIterator<T>;
    //friend ostream& operator<<(ostream& out, const Chain<T>& x); //加上这句代码会链接失败,
                                                                 //不明白为什么?虽然
                                                                 //Out()是公有的,<<可以访问,
                                                                 //但把Output设为私有再加上
                                                                 //这句代码也会报同样
                                                                 //的错误。
public:
    Chain() { first = 0; last = 0; }
    ~Chain();
    bool IsEmpty() const { return first == 0;}
    int Length() const;
    bool Find(int k, T& x) const;
    int Search(const T& x) const;
    Chain<T>& Delete(int k, T& x);
    Chain<T>& Insert(int k, const T& x);
    void Erase();
    void Zero() { first = 0; }  //类内定义,可能是内联函数,这种编程风格不好?
    Chain<T>& Append(const T& x);
    //void BinSort(int range);
    void BinSort(int range, int(*value)(T& x));
    void Output(ostream& out) const;
private:
    ChainNode<T> *first; //指向第一个节点的指针
    ChainNode<T> *last;  //指向最后一个节点的指针
};

template<class T>
Chain<T>::~Chain()
{
    // 链表的析构函数,用于删除链表中的所有节点
    last = 0;
    ChainNode<T> *next; // 下一个节点
    while (first) 
    {
        next = first->link;
        delete first;
        first = next;
    }
}

template<class T>
int Chain<T>::Length() const
{
    // 返回链表中的元素总数
    ChainNode<T> *current = first;
    int len = 0;
    while (current)
    {
        len++;
        current = current->link;
    }
    return len;
}

template<class T>
bool Chain<T>::Find(int k, T& x) const
{
    //寻找链表中的第k个元素,并将其传送至x
    //如果不存在第k个元素,则返回false,否则返回true
        if (k < 1) return false;
    ChainNode<T> *current = first;
    int index = 1; // current的索引
    while (index < k && current) 
    {
        current = current->link;
        index++;
    }
    if (current) { x = current->data; return true; }
    return false; // 不存在第k个元素
}

template<class T>
int Chain<T>::Search(const T& x) const
{
    //本函数假定对于类型T定义了 != 操作
    //寻找x,如果发现x,则返回x的地址
    //如果x不在链表中,则返回0
    ChainNode<T> *current = first;
    int index = 1; // current的索引
    while (current && current->data != x)
    {
        current = current->link;
        index++;
    }
    if (current)
        return index;
    return 0;
}

template<class T>
void Chain<T>::Output(ostream& out) const
{
    //本函数要求对于类型T必须定义<<操作
    //将链表元素送至输出流
    ChainNode<T> *current;
    for (current = first; current; current = current->link)
        out << current->data;// << " ";  把这个输出空格注释掉,输出的数据就不会在
                             //第二行开始行首多一个空格。
}

// 重载<<,该操作符声明为Chain类的友元
//试过将该操作符声明为成员,结果不凑效。
//想来可以理解,将如<<变成友元,那在主函数要怎么用?加点号!?
template <class T>
ostream& operator<<(ostream& out, const Chain<T>& C)
{
    //用法就是,cout << L (L是一个Chain链表),
    //从这里触发对Output()的调用,所以Output()是一个实用函数。
    C.Output(out); 
    return out;
}

template<class T>
Chain<T>& Chain<T>::Delete(int k, T& x)
{
    //把第k个元素取至x,然后从链表中删除第k个元素
    //如果不存在第k个元素,则引发异常OutOfBounds
        if (k < 1 || !first)
            throw OutOfBounds(); // 不存在第k个元素
    // p最终将指向第k个节点
    ChainNode<T> *p = first;
    // 将p移动至第k个元素,并从链表中删除该元素
    if (k == 1) // p已经指向第k个元素
        first = first->link; // 删除之
    else
    {
        //用q指向第k - 1个元素
        ChainNode<T> *q = first;
        for (int index = 1; index < k - 1 && q; index++)
            q = q->link;
        if (!q || !q->link)
            throw OutOfBounds(); //不存在第k个元素
        p = q->link; //存在第k个元素
        if (p == last) last = q; //对last指针的一种可能处理,
                                 //如果刚好删除最后一个元素
        q->link = p->link;  //从链表中删除该元素,如果p指向最后一个节点,
                            //则此处保证q->link=NULL
    }
    //保存第k个元素并释放节点p
    x = p->data;
    delete p;
    return *this;
};

template <class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
    //在第k个元素之后插入x
    //如果不存在第k个元素,则引发异常OutOfBounds
    //如果没有足够的空间,则传递NoMem异常
    if (k < 0) 
        throw OutOfBounds();
    //p最终将指向第k个节点
    ChainNode<T> *p = first;
    //将p移动至第k个元素
    for (int index = 1; index < k && p; index++)
        p = p->link;
    if (k > 0 && !p) 
        throw OutOfBounds(); //不存在第k个元素
    //插入
    ChainNode<T> *y = new ChainNode<T>;
    y->data = x;
    if (k) 
    {
        //在p之后插入
        y->link = p->link; //如果实在最后插入元素,
                           //那么此处可以保证y->link=NULL
        p->link = y;
    }
    else 
    {
        //作为第一个元素插入
        y->link = first;
        first = y;
    }
    if (!y->link) last = y; //对last指针的一种可能处理,
                            //如果刚好在最后的位置插入元素
    return *this;
};

template<class T>
void Chain<T>::Erase()
{
    //删除链表中的所有节点
    last = 0;
    ChainNode<T> *next;
    while (first)
    {
        next = first->link;
        delete first;
        first = next;
    }
};

template<class T>
Chain<T>& Chain<T>::Append(const T& x)
{
    // 在链表尾部添加x
    ChainNode<T> *y;
    y = new ChainNode<T>;
    y->data = x; y->link = 0;
    if (first)
    {
        //链表非空
        last->link = y;
        last = y;
    }
    else //链表为空
        first = last = y;
    return *this;
};

//假定Output()不是Chain类的成员函数,并且在该类中没有重载操作符<<。
//链表遍历器类
template<class T>
class ChainIterator 
{
public:
    T* Initialize(const Chain<T>& c)
    {
        location = c.first;
        if (location) return &location->data;
        return 0;
    }
    T* Next()
    {
        if(!location) return 0;
        location = location->link;
        if (location) return &location->data;
        return 0;
    }
private:
    ChainNode<T> *location;
};

//采用以上链表遍历器输出链表
//int *x;
//ChainIterator<int> c;
//x = c.Initialize(X);
//while (x) {
//  cout << *x << ' ';
//  x = c.Next();
//}
//cout << endl;

BinSort函数,该函数是 Chain<T> 类模板的成员函数,可与上面的代码放在同一个文件中。

#pragma once
#include <iostream>
#include "ChainList.h"
using namespace std;

类的定义应该放在cpp文件中
class Node
{
    friend ostream& operator<<(ostream&, const Node &);
public:
    int operator !=(Node x) const
    {
        return (exam1 != x.exam1 || exam2 != x.exam2
            || exam3 != x.exam3 || name[0] != x.name[0]);
    }
private:
    int exam1,exam2,exam3;
    char *name;
};
ostream& operator<<(ostream& out, const Node& x)
{
    out << x.exam1 << ' ' << x.exam2 << ' ' << x.exam3 << x.name[0] << ' ';
    return out;
}

//可以按不同域进行排序的BinSort版本。
//该版本多增加一个参数value,可以指定Node的不同域。
//通过定义不同版本的value的实参来实现。

template<class T>
void Chain<T>::BinSort(int range,int(*value)(T& x))
{
    // 按分数排序
    int b; // 箱子索引号
    ChainNode<T> **bottom, **top;
    //箱子初始化
    //数组中装着的元素是指向Chain<Node>类型的指针
    bottom = new ChainNode<T>*[range + 1];//这个数组中装着的元素是
                                          //指向每个箱子尾部ChainNode
                                          //节点的指针
    top = new ChainNode<T>*[range + 1];//这个数组中装着的元素是
                                       //指向每个箱子首部ChainNode
                                       //节点的指针(底为首,顶为尾)
    for (b = 0; b <= range; b++)
        bottom[b] = 0;
    // 把节点分配到各箱子中
    for (; first; first = first->link)//排序后Chain大链不输出的问题所在
                                      //因为BinSort之后,大链的first指针
                                      //指向NULL,而Chain类中的<<操作符
                                      //重载需要用到指向Chain大链链头的
                                      //first指针,这就产生矛盾了。
    {
        // 添加到箱子中
        //此处做此修改
        b = value(first->data); //first指向一个ChainNode节点,ChainNode节点
                                //的数据域应该是Node类型,b是一个int型。
                                //value是一个函数指针,这里具体由F1、F2、F3
                                //替换。在主函数中将这些函数传入BinSort时并
                                //没有给它们传递参数。这些函数的实参是
                                //这个for循环中传递的。
        if (bottom[b])
        {
            //箱子非空
            top[b]->link = first;//把first所指节点添加到顶部,
            top[b] = first;      //并把顶部节点指针top[b]指向
                                 //这个新添加的节点
        }
        else // 箱子为空
            bottom[b] = top[b] = first;
    }
    //收集各箱子中的元素,产生一个排序链表
    //只需将各个箱子中的链链接起来即可
    ChainNode<T> *y = 0;
    for (b = 0; b <= range; b++)
        if (bottom[b])
        {
            //箱子非空
            if (y) // 不是第一个非空的箱子
                y->link = bottom[b];
            else   // 第一个非空的箱子
            {
                y = bottom[b];
                first = bottom[b];  //第一个非空箱子在for循环中只会遇到一次
            }

            y = top[b];  //把y指向当前大链表尾节点,
                         //为链接下个箱子的链作准备
        }
    if (y) y->link = 0;
    //first = bottom[0];  //增加了这句代码,将first指针指向排好序的Chain大链链头
                        //然而对于三次成绩相加的情况,first就不一定要指向bottom[0]了
                        //所以,应该改为first指向第一个非空箱子的底部节点。
    delete[]bottom;
    delete[]top;
}

Node类和主函数。

#include <iostream>
#include <string>
#include <iomanip>     //可以指定输出格式
#include "ChainList.h"
#include "BinSortBeta.h"
#include "Xcept.h"

using namespace std;

class Node
{
    friend ostream& operator<<(ostream&, const Node &); //满足Chain<T>对类型T的要求
    friend int F1(Node& x), F2(Node& x), F3(Node& x);
    friend void main(void);
public:
    int operator !=(Node x) const
    {
        return (exam1 != x.exam1 || exam2 != x.exam2
            || exam3 != x.exam3 || name != x.name);
    }
private:
    int exam1;
    int exam2;
    int exam3;
    string name; //原作为char* name,这样的用法,编译通过,但是调试时会异常中断。
                 //在主函数给x.name赋值时中断
};
ostream& operator<<(ostream& out, const Node& x)
{
    out << setw(3) << x.exam1 << " " << setw(3) << x.exam2 << " "
        << setw(3) << x.exam3 << " " << x.name << endl;
    return out;
}

inline int F1(Node& x) { return x.exam1; }
inline int F2(Node& x) { return x.exam2; }
inline int F3(Node& x)
{
    return (x.exam1 + x.exam2 + x.exam3);
}
void main(void)
{
    Node p;
    Chain<Node> L;
    srand(1);
    for (int i = 1; i <= 20; i++)
    {
        p.exam1 = i / 2;           //range=10
        p.exam2 = 20 - i;          //range=20
        p.exam3 = rand()%101;      //这里可以产生0到100的随机数
        p.name = 64 + i;  //原作是x.name = i;
        L.Insert(0, p);
    }
    cout << L << endl;//加上此句代码,输出原顺序,输出成功。
                      //说明原先无法输出数据的原因极有可能出在BinSort函数上。
    L.BinSort(10, F1);
    cout << "Sort on exam 1" << endl;
    cout << L << endl;
    L.BinSort(20, F2);
    cout << "Sort on exam 2" << endl;
    cout << L << endl;
    L.BinSort(130, F3);  //这里range并不是最大的那个随机数,即100了
                         //因为这里是要输出三次成绩相加,所以range值大于100,
                         //range应该设为三次成绩各个最大值相加,即130
    cout << "Sort on sum of exams" << endl;
    cout << L << endl;
}

还有一个包含异常处理代码的文件可以查看这里

这里说明一下,本文的代码是参考了来自网上的电子资料。书中的原代码重在起到说明作用而并没有考虑到实现的合理性。因此,本文在原来的基础上对部分代码进行了修改,使之后能够运行并得出结果,但是测试数据有限,不能保证所有情况都能可靠运行。在学习、修改原代码的过程中,小编获益良多。部分感想注释在了代码中,由于代码编译产生的错误并不是按照一定的顺序来的(或者是但是我没发现),所以我的注释会显得杂乱无章。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值