该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
________________________}
________________}
________________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();
}
=======================================================================