首页新闻找找看学习计划

C++基数排序问题

0
悬赏园豆:10 [已解决问题] 解决于 2014-09-04 16:55

这是个一类实现的基数排序问题,出了错,求帮忙看看哪里有错

 

#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;

#define maxr 10
#define maxd 8
class Radix
{
typedef struct link
{
char data[maxd];
link *next;
}RecType;
RecType *p;
public:
Radix(){ p = NULL; }
Radix(char *a[], int n);
~Radix(){}
friend void DispLink(Radix r);
void RadixSort(int r, int d);
};

void DispLink(Radix r)
{
while (r.p != NULL)
{
cout << r.p->data << " ";
r.p = r.p->next;
}
cout << endl;
}
void Radix::RadixSort(int r, int d)
{
RecType *head[maxr], *tail[maxr], *t;
int i, j, k;
for (i = d - 1; i >= 0; i--)
{
for (j = 0; j < r; j++)
head[j] = tail[j] = NULL;
while (p != NULL)
{
k = p->data[i] - '0';
if (head[k] == NULL)
{
head[k] = p;
tail[k] = p;
}
else
{
tail[k]->next = p;
tail[k] = p;
}
p = p->next;
}
p = NULL;
for (j = 0; j < r; j++)
if (head[j] != NULL)
{
if (p == NULL)
{
p = head[j];
t = tail[j];
}
else
{
t->next = head[j];
t = tail[j];
}
}
//t->next = NULL;
cout << "order by " << d - i - 1 << " bit ";
//DispLink();
}
}

Radix::Radix(char *a[], int n)
{
int i;
RecType *s, *t = NULL;
for (i = 0; i < n; i++)
{
s = (RecType*)malloc(sizeof(RecType));
strcpy_s(s->data, a[i]);
if (i == 0)
{
p = s;
t = s;
}
else
{
t->next = s;
t = s;
}
}
t->next = NULL;
}
void main()
{

char *a[10] = { "23", "24", "32", "56", "67", "45", "87", "98", "48", "65" };
Radix r(a,10);
DispLink(r);
r.RadixSort(10,2);
DispLink(r);
}

学海没有鱼的主页 学海没有鱼 | 初学一级 | 园豆:5
提问于:2014-09-03 18:31
< >
分享
最佳答案
0
// Radix.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;

#define maxr 10
#define maxd 8

//定义到外面,方便DispLink使用,或者把DispLink作为Radix的内置函数
typedef struct link
{
    char data[maxd];
    link *next;
}RecType;

class Radix
{
    RecType *p;
public:
    Radix(){ p = NULL; }
    Radix(char *a[], int n);
    ~Radix(){}
    friend void DispLink(Radix r);
    void RadixSort(int r, int d);
};

void DispLink(Radix r)
{
    //而直接使用r.p,破坏了原有数据,应该使用临时变量指针保留r的现场
    RecType *t = r.p;
    while (t != NULL)
    {
        cout << t->data << " ";
        t = t->next;
    }
    cout << endl;
}
void Radix::RadixSort(int r, int d)
{
    RecType *head[maxr], *tail[maxr], *t;
    int i, j, k;
    for (i = d - 1; i >= 0; i--)
    {
        for (j = 0; j < r; j++)
            head[j] = tail[j] = NULL;
        while (p != NULL)
        {
            k = p->data[i] - '0';
            if (head[k] == NULL)
            {
                head[k] = p;
                tail[k] = p;
            }
            else
            {
                tail[k]->next = p;
                tail[k] = p;
            }
            p = p->next;
        }
        p = NULL;
        for (j = 0; j < r; j++)
        {
            if (head[j] != NULL)
            {
                if (p == NULL)
                {
                    p = head[j];
                    t = tail[j];
                }
                else
                {
                    t->next = head[j];
                    t = tail[j];
                }
            }
        }
        t->next = NULL;//缺少该语句,导致死循环
        cout << "order by " << d - i - 1 << " bit ";
        DispLink(*this);
    }
}

Radix::Radix(char *a[], int n)
{
    int i;
    RecType *s, *t = NULL;
    for (i = 0; i < n; i++)
    {
        s = (RecType*)malloc(sizeof(RecType));
        strcpy_s(s->data, a[i]);
        if (i == 0)
        {
            p = s;
            t = s;
        }
        else
        {
            t->next = s;
            t = s;
        }
    }
    t->next = NULL;
}

void main()
{
    char *a[10] = { "23", "24", "32", "56", "67", "45", "87", "98", "48", "65" };
    Radix r(a,10);
    DispLink(r);
    r.RadixSort(10,2);
    DispLink(r);
}
收获园豆:10
519740105 | 大侠五级 |园豆:5810 | 2014-09-04 07:17

回答的很好,多谢指正

学海没有鱼 | 园豆:5 (初学一级) | 2014-09-04 16:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册