C語言中數(shù)據(jù)結(jié)構(gòu)之鏈式基數(shù)排序
更新時間:2017年09月14日 08:45:50 作者:Vit_rose
這篇文章主要介紹了C語言中數(shù)據(jù)結(jié)構(gòu)之鏈式基數(shù)排序的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
C語言中數(shù)據(jù)結(jié)構(gòu)之鏈式基數(shù)排序
實現(xiàn)效果圖:

實例代碼:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status;
typedef int ElemType;
#define MAX_NUM_OF_KEY 8 //關(guān)鍵字項數(shù)最大值
#define RADIX 10 //關(guān)鍵字基數(shù),此時是十進制整數(shù)的基數(shù)
#define MAX_SPACE 100 //書上為10000
#define ord(ch) ((ch)-'0')
#define succ(x) ((x)+1)
typedef char KeyType;
typedef struct
{
KeyType keys[MAX_NUM_OF_KEY]; //關(guān)鍵字
int next;
}SLCell; //靜態(tài)鏈表的結(jié)點類型
typedef struct
{
SLCell r[MAX_SPACE]; //靜態(tài)鏈表的可利用空間,r[0]為頭結(jié)點
int keynum; //記錄當前關(guān)鍵字個數(shù)
int recnum; //靜態(tài)鏈表的當前長度
}SLList; //靜態(tài)鏈表類型
typedef int ArrType[RADIX]; //指針數(shù)組類型
/*******************************聲明部分****************************************/
/*******************************函數(shù)部分****************************************/
void Distribute(SLCell r[],int i,ArrType f,ArrType e)
{
int j,p;
for(j = 0;j<RADIX;++j){
f[j] = 0;
e[j] = 0;
}
for(p = r[0].next; p ;p = r[p].next){
j = ord(r[p].keys[i]);
if(!f[j])
f[j] = p;
else
r[e[j]].next = p;
e[j] = p;
}
}
void Collect(SLCell r[],int i,ArrType f,ArrType e)
{
int j,t;
for(j = 0; j<RADIX&&!f[j] ; j = succ(j)); //找到第一個非空子表,succ為求后繼函數(shù)
if(j<RADIX){
r[0].next = f[j];
t = e[j];
while(j<RADIX){
for(j = succ(j) ; j<RADIX-1 && !f[j]; j = succ(j));
if(f[j] && j<=RADIX-1){
r[t].next = f[j];
t = e[j];
}
}
r[t].next = 0;
}
}
void RadixSort(SLList *L)
{
int i;
ArrType f,e;
for(i = 0;i<L->keynum;i++){
Distribute(L->r,i,f,e);
Collect(L->r,i,f,e);
}
}
void CreateSLL(SLList *L)
{
char s[100];
int i,n,ct;
L->recnum = 0;
/* printf("請輸入關(guān)鍵字個數(shù):\n");
scanf("%d",&L->keynum);
printf("請輸入鏈表長度:\n");
scanf("%d",&n);*/
L->keynum = 3;
n = 10;
printf("依次輸入:278 109 063 963 589 184 505 269 008 083 \n");
for(ct = 0;ct<n;ct++){
// printf("請輸入關(guān)鍵字:\n");
scanf("%s",&s);
L->recnum++;
for(i = 0;i<L->keynum;++i)
L->r[L->recnum].keys[L->keynum-1-i] = s[i];
}
for(i = 0;i<L->recnum;++i)
L->r[i].next = i+1;
L->r[L->recnum].next = 0;
}
void TraverseSLL(SLList L)
{
int i,j;
for(i = L.r[0].next; i ;i = L.r[i].next){
for(j = L.keynum-1;j>=0;j--)
printf("%c",L.r[i].keys[j]);
printf(" ");
}
printf("\n");
}
/*******************************主函數(shù)部分**************************************/
int main()
{
SLList L;
printf("創(chuàng)建靜態(tài)鏈表\n");
CreateSLL(&L);
printf("創(chuàng)建完成:\n");
TraverseSLL(L);
printf("\n基數(shù)排序:\n");
RadixSort(&L);
TraverseSLL(L);
return 0;
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
相關(guān)文章
C++異常處理 try,catch,throw,finally的用法
這篇文章主要介紹了C++異常處理 try,catch,throw,finally的用法,需要的朋友可以參考下2018-01-01
FFmpeg實現(xiàn)變速播放的兩種方法總結(jié)
這篇文章主要為大家詳細介紹了FFmpeg中實現(xiàn)變速播放的兩種方法,文中的示例代碼講解詳細,具有一定的學(xué)習價值,感興趣的可以了解一下2023-07-07
c++中關(guān)于max_element()函數(shù)解讀
這篇文章主要介紹了c++中關(guān)于max_element()函數(shù)解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
詳解C++中的ANSI與Unicode和UTF8三種字符編碼基本原理與相互轉(zhuǎn)換
在C++編程中,我們有時需要去處理字符串編碼的相關(guān)問題,常見的字符編碼有ANSI窄字節(jié)編碼、Unicode寬字節(jié)編碼及UTF8可變長編碼。很多人在處理字符串編碼問題時都會有疑惑,即便是有多年工作經(jīng)驗的朋友也可能搞不清楚。所以有必要講一下這三種字符編碼以及如何去使用它們2021-11-11

