所有原创文章转载请注明作者及链接
// blackboycpp(AT)gmail.com
// QQ群: 135202158
/******************************************************************** File: SinglyLinkedList.h Author: blackboy, [email protected] Purpose: 单链表ADT,C实现 Created: 2011-03-29 Modified: 2011-03-31 08:54 *********************************************************************/ #ifndef __SINGLY_LINKED_LIST_H__ #define __SINGLY_LINKED_LIST_H__ typedef int ElementType; struct Node; typedef struct Node* PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; struct Node { ElementType Val; Position Next; }; void CreateList(List*); void DeleteList(List); int IsEmpty(List); int IsLast(Position, List); Position Header(List); Position First(List); Position Advance(Position); Position Find(ElementType, List); ElementType GetAt(Position); void MakeEmpty(List); void Delete(ElementType, List); void Insert(ElementType, Position, List); void InsertFront(ElementType, List); void InsertBack(ElementType, List); #endif
#include <stdlib.h> #include "SinglyLinkedList.h" void CreateList(List* L) { PtrToNode pHeader = (struct Node*)malloc(sizeof(struct Node)); pHeader->Next = NULL; *L = pHeader; } void DeleteList(List L) { MakeEmpty(L); free(L); } int IsEmpty(List L) { return L->Next == NULL; } int IsLast(Position P, List L) { return P->Next == NULL; } Position Header(List L) { return L; } Position First(List L) { return L->Next; } Position Advance(Position P) { return P->Next; } Position Find(ElementType X, List L) { Position p = L->Next; while(p != NULL && p->Val != X) { p = p->Next; } return p; } ElementType GetAt(Position P) { return P->Val; } void MakeEmpty(List L) { Position p, tmp; p = L->Next; while(p != NULL) { tmp = p->Next; free(p); p = NULL; p = tmp; } L->Next = NULL; } void Delete(ElementType X, List L) { Position p, tmp; p = L; while(p->Next != NULL && p->Next->Val != X) { p = p->Next; } if(!IsLast(p, L)) { tmp = p->Next; p->Next = tmp->Next; free(tmp); tmp = NULL; // 错误示范: //free(p->Next); //p->Next = tmp->Next; } } void Insert(ElementType X, Position P, List L) { PtrToNode pNode; pNode = (struct Node*)malloc(sizeof(struct Node)); pNode->Val = X; pNode->Next = P->Next; P->Next = pNode; } void InsertFront(ElementType X, List L) { Position pos; PtrToNode pNode; pos = L; pNode = (struct Node*)malloc(sizeof(struct Node)); pNode->Val = X; pNode->Next = pos->Next; pos->Next = pNode; } void InsertBack(ElementType X, List L) { Position pos; PtrToNode pNode; // move to tail pos = L; while(pos->Next != NULL) pos = pos->Next; pNode = (struct Node*)malloc(sizeof(struct Node)); pNode->Val = X; pNode->Next = pos->Next; pos->Next = pNode; }
/******************************************************************** File: RadixSort.c Author: blackboy, [email protected] Purpose: 基数排序, 桶由单链表实现 Created: 2011-03-30 Modified: 2011-03-31 08:56 *********************************************************************/ #include <stdio.h> #include <stdlib.h> #include "SinglyLinkedList.h" #define N 10 // 排序的数个数 #define B 10 // 桶数,即基数 #define P 3 // 位数 void RadixSort(int arr[]); int GetDigit(int x, int y); void PrintArray(int arr[], int size); int main() { int i; //int arr[N]; int arr[N] = {64, 8, 216, 512, 27, 729, 0, 1, 343, 125}; // 10, 3 //int arr[N] = {64, 8, 216, 512, 125, 729, 0, 729, 343, 125}; //int arr[N] = {12, 58, 37, 64, 52, 36, 99, 63, 18, 9, 20, 88, 47}; // 13, 2 printf("before sort: "); PrintArray(arr, N); RadixSort(arr); printf("after sort: "); PrintArray(arr, N); system("PAUSE"); return 0; } void RadixSort(int arr[]) { int i, j, k, digit; Position pos; List bucket[B]; // 创建桶 for(i=0; i<B; ++i) CreateList(&bucket[i]); // 从低位到高位循环每一位数字 for(i=0; i<P; ++i) { // 将桶置空 for(j=0; j<B; ++j) MakeEmpty(bucket[j]); // 根据当前位上的数字,将之放入对应桶中。并注意顺序(后进的放在后面) for(j=0; j<N; ++j) { digit = GetDigit(arr[j], i); InsertBack(arr[j], bucket[digit]); } k = 0; // 将每趟排序后的结果存回arr for(j=0; j<B; ++j) { pos = First(bucket[j]); while(pos != NULL) { arr[k++] = pos->Val; pos = pos->Next; } } } for(i=0; i<B; ++i) DeleteList(bucket[i]); } // 取整数x的倒数第y位数字,y从0开始 int GetDigit(int x, int y) { int i, t = 1; for(i=0; i<=y; ++i) t *= 10; return (x % t) / (t / 10); } void PrintArray(int arr[], int size) { int i; for(i=0; i<size; ++i) { printf("%d ", arr[i]); } printf("/n"); }