基数排序算法c语言,【图片】基数排序的实现->Demo program!【c语言吧】_百度贴吧...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

________________________}

________________}

________________putchar('\n');

________}

}

/* 打印结果 */

void PrintResult(List Bucket[])

{

________int i, cnt = 0;

________Position P;

________for (i = 0; i < BUCKETCNT; ++i) {

________________P = Bucket[i] -> Next;

________________while (P != NULL) {

________________________printf("%5d", P -> Element);

________________________P = P -> Next;

________________________cnt++;

________________________if (cnt % 10 == 0)

________________________________putchar('\n');

________________}

________}

________putchar('\n');

}

/* 计算需要进行多少次桶排序 */

int ComputeTimes(int a[], int sz)

{

________int i, Max, cnt;

________Max = a[0];

________for (i = 1; i < sz; ++i)

________________if (a[i] > Max)

________________________Max = a[i];

________cnt = 1;

________printf("\nThe maxinum is %d.\n", Max);

________while (Max /= 10, Max != 0)

________________cnt++;

________return cnt;

}

/* 清理工作 */

void FreeList(List Bucket[])

{

________int i;

________for (i = 0; i < BUCKETCNT; i++)

________________DeleteList(Bucket[i]);

}

=============================================================================

list.h

=============================================================================

#include

#include

#ifndef _List_h

struct Node;

typedef struct Node *PtrToNode;

typedef PtrToNode List;

typedef PtrToNode Position;

typedef int ElementType;

int IsLast(Position P);

void FatalError(void);

void Insert(ElementType X, Position P);

void Delete(ElementType X, List L);

void DeleteList(List L);

List Creat(void);

Position FindPrevious(ElementType X, List L);

Position Compare(ElementType X, List L);

#endif

struct Node {

________ElementType Element;

________Position Next;

};

===============================================================================

list.c

=============================================================================

/* 错误信息函数 */

void FatalError(void)

{

________fprintf(stderr, "Out of space - bye!\n");

________exit(1);

}

int IsLast(Position P)

{

________return P -> Next == NULL;

}

void Insert(ElementType X, Position P)

{

________Position TmpCell;

________TmpCell = malloc(sizeof(struct Node));

________if (TmpCell == NULL)

________________FatalError();

________TmpCell -> Element = X;

________TmpCell -> Next = P -> Next;

________P -> Next = TmpCell;

}

void Delete(ElementType X, List L)

{

________Position P, TmpCell;

________P = FindPrevious(X, L);

________if (!IsLast(P)) {

________________TmpCell = P -> Next;

________________P -> Next = TmpCell -> Next;

________________free(TmpCell);

________}

}

Position FindPrevious(ElementType X, List L)

{

________Position P;

________P = L;

________while (P -> Next != NULL && P -> Next -> Element != X)

________________P = P -> Next;

________return P;

}

void DeleteList(List L)

{

________Position P, Tmp;

________P = L -> Next;

________L -> Next = NULL;

________while (P != NULL) {

________________Tmp = P -> Next;

________________free(P);

________________P = Tmp;

________}

}

/* 进行对比, 以排序方式加入表中 */

Position Compare(ElementType X, Position P)

{

________Position Q;

________Q = P;

________while (!IsLast(Q) && Q -> Next -> Element < X)

________________Q = Q -> Next;

________return Q;

}

/* 建立表头 */

List Creat(void)

{

________List L;

________L = malloc(sizeof(struct Node));

________if (L != NULL) {

________________L -> Next = NULL;

________________return L;

________}

________else

________________FatalError();

}

=======================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值