C語(yǔ)言中數(shù)據(jù)結(jié)構(gòu)之鏈?zhǔn)交鶖?shù)排序
實(shí)現(xiàn)效果圖:

實(shí)例代碼:
#include<stdio.h>#include<string.h>#include<stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1typedef int Status;typedef int ElemType;#define MAX_NUM_OF_KEY 8 //關(guān)鍵字項(xiàng)數(shù)最大值#define RADIX 10 //關(guān)鍵字基數(shù),此時(shí)是十進(jìn)制整數(shù)的基數(shù)#define MAX_SPACE 100 //書(shū)上為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é)點(diǎn)類(lèi)型typedef struct{ SLCell r[MAX_SPACE]; //靜態(tài)鏈表的可利用空間,r[0]為頭結(jié)點(diǎn) int keynum; //記錄當(dāng)前關(guān)鍵字個(gè)數(shù) int recnum; //靜態(tài)鏈表的當(dāng)前長(zhǎng)度}SLList; //靜態(tài)鏈表類(lèi)型typedef int ArrType[RADIX]; //指針數(shù)組類(lèi)型/*******************************聲明部分****************************************//*******************************函數(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)); //找到第一個(gè)非空子表,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("請(qǐng)輸入關(guān)鍵字個(gè)數(shù):/n"); scanf("%d",&L->keynum); printf("請(qǐng)輸入鏈表長(zhǎng)度:/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("請(qǐng)輸入關(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;}如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選