基于链表的基数排序v2.0

数据结构与算法分析——c语言描述 第三章的基数排序

之前写过了,但当时发现时间主要用在找到最后一个元素,今天重新写了,保存一个指向最后结点的指针。

last指针的初始值要非常小心啊,一开始我是初始值为NULL。这样有一个问题,就是插入第一个元素的时候(头结点版链表),last指针是不会更新的!islast函数我是判断是否和last指针相等。debug了一整天,一开始想都想不明白,后来才大概摸到方向。智商捉急。。。。


list.h

typedef int ElementType;
#ifndef _List_H
#define _List_H

struct Node;
struct ListRecord;
typedef struct ListRecord *VariantList;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;


VariantList createVariantList();
void MakeEmpty(VariantList varList);
int IsEmpty(VariantList varList);
int IsLast(Position P, VariantList varList);
Position Find(ElementType X, VariantList varList);
void Delete(ElementType X, VariantList varList);
Position FindPrevious(ElementType X, VariantList varList);
void Insert(ElementType X, VariantList varList, Position P);
void DeleteVariantList(VariantList varList);
Position Header(VariantList varList);
Position First(VariantList varList);
Position Advance(Position P);
ElementType Retrieve(Position P);
void InsertFront(ElementType X, VariantList varList);
void InsertBack(ElementType X, VariantList varList);
#endif


list.c

#include"list.h"
#include"fatal.h"

struct ListRecord {
	List L;
	PtrToNode Last;
};

struct Node
{
	ElementType Element;
	Position Next;
};

static void DeleteList(List L) {
	Position p;
	p = L->Next;
	L->Next = NULL;
	while (p != NULL) {
		Position tmp;
		tmp = p->Next;
		free(p);
		p = tmp;
	}
}


VariantList createVariantList() {
	VariantList varList = malloc(sizeof(struct ListRecord));
	varList->L = malloc(sizeof(struct Node));
	if (varList->L == NULL)
		Error("out of memory");
	varList->L->Next = NULL;
	varList->Last = varList->L;
	return varList;
}

void MakeEmpty(VariantList varList) {
	if (varList->L != NULL)
		DeleteList(varList->L);
	
	varList->Last = varList->L;
}

int IsEmpty(VariantList varList) {
	return varList->L->Next == NULL;
}

int IsLast(Position P, VariantList varList) {
	return P == varList->Last;
}

Position Find(ElementType X, VariantList varList) {
	Position P;
	P = varList->L->Next;
	while (P != NULL && P->Element != X)
	{
		P = P->Next;
	}
	return P;
}

void Delete(ElementType X, VariantList varList) {
	Position P;
	P = FindPrevious(X, varList);
	if (!IsLast(P, varList)) {
		Position TmpCell = P->Next;
		if (IsLast(TmpCell,varList))
			varList->Last = P;
		P->Next = TmpCell->Next;
		free(TmpCell);
	}
}

Position FindPrevious(ElementType X, VariantList varList) {
	Position P;
	P = varList->L;
	while (P->Next != NULL&&P->Next->Element != X)
		P = P->Next;
	return P;
}

void Insert(ElementType X, VariantList varList, Position P) {
	Position tmpCell;
	tmpCell = malloc(sizeof(struct Node));
	if (tmpCell == NULL)
		FatalError("Out of space!!");
	tmpCell->Element = X;
	tmpCell->Next = P->Next;
	if (IsLast(P,varList))
		varList->Last = tmpCell;
	P->Next = tmpCell;
}

void DeleteVariantList(VariantList varList) {
	MakeEmpty(varList);
	free(varList->L);
	free(varList);
}

Position Header(VariantList varList) {
	return varList->L;
}

Position First(VariantList varList) {
	return varList->L->Next;
}

Position Advance(Position P) {
	return P->Next;
}

ElementType Retrieve(Position P) {
	return P->Element;
}

void InsertFront(ElementType X, VariantList varList) {
	Insert(X, varList, varList->L);
}

void InsertBack(ElementType X, VariantList varList) {
	if (IsEmpty(varList)) {
		Insert(X, varList, varList->L);
	}
	else
	{
		Insert(X, varList, varList->Last);
	}
}


main.c

#include"list.h"
#include<stdio.h>
#define BUCKETS 10 //桶的数量
#define N 10//需要排序数字数量
#define BITS 3 //位数

void radixSort(int arr[]);
int getDigital(int x, int cnt);
void print(int arr[]);

int main() {
	int arr[N] = { 64, 8, 216, 512, 27, 729, 0, 1, 343, 125 };
	print(arr);
	radixSort(arr);
	print(arr);
}

void radixSort(int arr[]) {
	VariantList bucket[BUCKETS];
	int i, j, k;
	for (i = 0; i < BUCKETS; i++) {
		bucket[i] = createVariantList();
	}
	
	for (i = 0; i < BITS; i++) {
		
		for (j = 0; j < BUCKETS; j++)
			MakeEmpty(bucket[j]);
		
		for (k = 0; k < N; k++) {
			
			InsertBack(arr[k], bucket[getDigital(arr[k], i)]);
			
		}
		
		for (int i = 0, k = 0; i < N; i++) {
			Position p;
			p = First(bucket[i]);
			while (p != NULL) {
				arr[k++] = Retrieve(p);
				p = Advance(p);
			}
		}
	}
	
	for (i = 0; i<BUCKETS; ++i)
		DeleteVariantList(bucket[i]);
}

int getDigital(int x, int cnt) {
	for (int i = 0; i < cnt; i++)
		x /= 10;
	return x % 10;
}

void print(int arr[]) {
	for (int i = 0; i < N; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值