⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8-4.txt

📁 数据结构源程序
💻 TXT
字号:
/*哈希查找的基本运算与实现*/
#include <stdio.h>
#include <malloc.h>
#define HASHKEY 11
typedef int keytype;
typedef struct
{
	keytype num;	
}datatype;
typedef struct
{
	datatype *data;
	int length;
}S_TBL;
int Hash(datatype kx);
int CreateHashTbl(S_TBL *tbl);
int MovElemToHashTbl(datatype kx,S_TBL *tbl,int h_addr);
void main()
{
	int i,flag;
	S_TBL *tbl=(S_TBL *)malloc(sizeof(S_TBL));
	printf("please input table length:\n");
	scanf("%d",&(tbl->length));
	tbl->data=(datatype *)calloc( tbl->length, sizeof(datatype) );
	for(i=0;i<tbl->length;i++)
	{
		tbl->data[i].num = -100;
	}
	flag = CreateHashTbl(tbl);
	if(flag==1)
	{
		for(i=0;i<tbl->length;i++)
		{
			printf("%6d",tbl->data[i].num);
		}
		printf("\n\n");
	}
	else
	{
		printf("Create Hash_Table Failed!\n");
	}	
}
int Hash(datatype kx)
{
	int addr;
	addr = kx.num%HASHKEY;
	return addr;
}
int CreateHashTbl(S_TBL *tbl)
{
	int addr,finished=0;
	datatype kx;
	printf("Please Input Value of KeyNumber:"); 
	scanf("%d",&(kx.num));
	while(kx.num != 0)
	{
		addr = Hash(kx);
		finished = MovElemToHashTbl(kx,tbl,addr);
		if(finished == 0)
		{
			break;
		}
		printf("Please continue Input Value of KeyNumber:"); 
		scanf("%d",&(kx.num));
	}//end while
	return finished;
}
int MovElemToHashTbl(datatype kx,S_TBL *tbl,int h_addr)
{
	int count = 0,status = 0;
	while((tbl->data[h_addr].num)!=-100)
	{
		h_addr=(h_addr+1)%HASHKEY;
		count++;
		if(count==HASHKEY-1)
		{
			break;
		}
	}//end while
	if(count<HASHKEY-1)
	{
		tbl->data[h_addr] = kx;
		status = 1;
	}
	return status;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -