代码如下:
#include <iostream>
#include <cmath>
using namespace std;
typedef int KeyType;
const int END = -1;
const int Radix = 10;typedef struct Node
{KeyType key;struct Node *next;
};Node *CreateList()
{KeyType x;Node *q = nullptr;cin >> x;while (x != END){Node *p = new Node;p->key = x;p->next = q;q = p;cin >> x;}return q;
}int FindDigits(Node *p)
{int max = -1;while (p){if (p->key > max) max = p->key;p = p->next;}int digits = 1;while (max / 10 > 0){digits++;max = max / 10;}return digits;
}void Distribute(Node *&p,int digits, Node *f[], Node *r[])
{Node *q;while (p){q = p;p = p->next;int k = q->key;int radix = static_cast<int>(k / pow(10, digits - 1)) % 10;if (r[radix] == nullptr){r[radix] = q;f[radix] = q;r[radix]->next = nullptr;}else{q->next = r[radix]->next;r[radix]->next = q;r[radix] = q;}}
}void Collect(Node *&p, Node *f[], Node *r[])
{p = nullptr;for (int i = Radix - 1; i >= 0; i--){if (f[i] != nullptr){r[i]->next = p;p = f[i];r[i] = nullptr;//不要忘记这两步!!!f[i] = nullptr;}}
}void RadixSort(Node *&p)
{Node *f[Radix], *r[Radix];for (int i = 0; i < Radix; i++){f[i] = nullptr;r[i] = nullptr;}int digits = FindDigits(p);for (int i = 1; i <= digits; i++){Distribute(p, i, f, r);Collect(p, f, r);}
}void PrintElem(Node *p)
{while (p){cout << p->key << " ";p = p->next;}cout << endl;
}int main()
{Node *p;p = CreateList();PrintElem(p);cout << FindDigits(p) << endl;RadixSort(p);PrintElem(p);return 0;
}