練習五: 散列(線性探查)

【基本要求】
編寫一個程序,實現哈希表的初始化、插入、查找、刪除等相關運算,並在此基礎上完成如下功能:
(1) 建立{16,74,60,43,54,90,46,31,29,88,77}哈希表A[0…12],哈希函數爲H(k)=key%p,(p取13),並採用線性探查法解決衝突。
(2) 在上述哈希表中查找關鍵字爲29的記錄。
(3) 在上述哈希表中刪除關鍵字爲77的記錄,再將其插入。
【輸出結果】
輸出結果例子如下:
哈希表地址: 0 1 2 3 4 5 6 7 8 9 10 11 12
哈希表關鍵字: 77 54 16 43 31 29 46 60 74 88 90
搜索次數: 2 1 1 1 1 4 1 1 1 1 1
平均搜索長度ASL(11)=1.36364
ha[6].key=29
刪除關鍵字77
哈希表地址: 0 1 2 3 4 5 6 7 8 9 10 11 12
哈希表關鍵字: 54 16 43 31 29 46 60 74 88 90
搜索次數: 1 1 1 1 4 1 1 1 1 1
平均搜索長度ASL(10)=1.3
未找到77
插入關鍵字77
哈希表地址: 0 1 2 3 4 5 6 7 8 9 10 11 12
哈希表關鍵字: 77 54 16 43 31 29 46 60 74 88 90
搜索次數: 2 1 1 1 1 4 1 1 1 1 1
平均搜索長度ASL(11)=1.36364

#include <stdio.h>
#include <stdlib.h>

enum State
{
	EXIST,
	EMPTY,
	DELETE
};

typedef struct HashCell
{
	int Key;
	int Searchtime;
	State Info;
};

typedef struct HashTable
{
	int TableSize;
	HashCell *TheCells;
}*Hash;

void InitHash(Hash H)
{
	H->TheCells = (HashCell*)malloc(sizeof(HashCell) * H->TableSize);
	for (int i = 0; i < H->TableSize; i++)
		H->TheCells[i].Info = EMPTY;
}

int InsertHash(Hash H, int Key)
{
	int Index, i, j = 0, searchtime = 0;
	Index = Key % H->TableSize;
	for (i = Index; i < H->TableSize; i++)
	{
		j++;
		searchtime++;
		if (H->TheCells[i].Info != EXIST)
			break;
		if (i == H->TableSize - 1)
			i = -1;
		if (j == H->TableSize)
			return -1;
	}
	H->TheCells[i].Info = EXIST;
	H->TheCells[i].Key = Key;
	H->TheCells[i].Searchtime = searchtime;
	return 0;
}

int FindHash(Hash H, int Key)
{
	int Index, i, j = 0;
	Index = Key % H->TableSize;
	for (i = Index; i < H->TableSize; i++)
	{
		j++;
		if (H->TheCells[i].Key == Key && H->TheCells[i].Info == EXIST)
			return i;
		if (i == H->TableSize - 1)
			i = -1;
		if (j == H->TableSize)
			return -1;
	}
	return 0;
}

int DeleteHash(Hash H, int Key)
{
	int Index = FindHash(H, Key);
	if (Index == 0)
		H->TheCells[Index].Info = DELETE;
	else
		return -1;
	return 0;
}

double ASL(Hash H)
{
	double j = 0, asl = 0;
	for (int i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info != EXIST)
			asl += 0;
		else
		{
			j++;
			asl += H->TheCells[i].Searchtime;
		}
	}
	asl /= j;
	return asl;
}

void PrintHash(Hash H)
{
	int i;
	printf("哈希表地址:       ");
	for (i = 0; i < H->TableSize; i++)
		printf("%d  ", i);
	printf("\n哈希表關鍵字:     ");
	for (i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info != EXIST)
			printf("   ");
		else
			printf("%d ", H->TheCells[i].Key);
	}
	printf("\n搜索次數:          ");
	for (i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info != EXIST)
			printf("   ");
		else
			printf("%d  ", H->TheCells[i].Searchtime);
	}
	printf("\n平均搜索長度ASL(11)= %lf\n", ASL(H));
}

int main()
{
	Hash H;
	H = (Hash)malloc(sizeof(HashTable));
	H->TableSize = 13;
	InitHash(H);	
	int data[] = { 16,74,60,43,54,90,46,31,29,88,77 };
	for (int i = 0; i < 11; i++)
		InsertHash(H, data[i]);
	PrintHash(H);
	printf("ha[%d].key=29\n", FindHash(H,29));
	printf("刪除關鍵字77\n");
	(DeleteHash(H, 77) == 0) ? PrintHash(H) : printf("刪除失敗!\n");
	(FindHash(H, 77) == 0) ? PrintHash(H) : printf("未找到77!\n");
	(InsertHash(H,77) == 0) ? PrintHash(H) : printf("插入失敗!!\n");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章