箱子排序

先上一幅圖片

這裏寫圖片描述

這幅圖就概括了箱子排序的大致過程。

在箱子排序的過程中,使用了 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;
}

還有一個包含異常處理代碼的文件可以查看這裏

這裏說明一下,本文的代碼是參考了來自網上的電子資料。書中的原代碼重在起到說明作用而並沒有考慮到實現的合理性。因此,本文在原來的基礎上對部分代碼進行了修改,使之後能夠運行並得出結果,但是測試數據有限,不能保證所有情況都能可靠運行。在學習、修改原代碼的過程中,小編獲益良多。部分感想註釋在了代碼中,由於代碼編譯產生的錯誤並不是按照一定的順序來的(或者是但是我沒發現),所以我的註釋會顯得雜亂無章。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章