Hash表

#include <iostream>
#include <string.h>
using namespace std;

const int NAME_NO = 20;
const int TABLE_SIZE = 50;

typedef struct 
{
	char *py;
	int sum;
}Name;

typedef struct
{
	char *py;
	int sum;
	int si;
}Hash;

Name NameList[NAME_NO];
Hash HashList[TABLE_SIZE];

void init_name_table()
{
	NameList[0].py = "zhoujielun";
	NameList[1].py = "naying";
	NameList[2].py = "halin";	
	NameList[3].py = "wangfeng";
	NameList[4].py = "fenghanzao";
	NameList[5].py = "fuzongkai";
	NameList[6].py = "hujinbin";
	NameList[7].py = "huangjiangwu";
	NameList[8].py = "lilongqiang";
	NameList[9].py = "guoxiaodong";
	NameList[10].py = "liuqing";
	NameList[11].py = "zhongqiang";
	NameList[12].py = "shenglong";
	NameList[13].py = "chulingling";
	NameList[14].py = "chengpeisi";
	NameList[15].py = "liaoxiaobin";
	NameList[16].py = "fenggong";
	NameList[17].py = "shunzi";
	NameList[18].py = "shihui";
	NameList[19].py = "lixiaolong";
	NameList[20].py = "zhouxingxing";

	for (int i=0; i<NAME_NO; i++)
	{
		int sum = 0;
		for (int j=0; j<strlen(NameList[i].py); j++)	
		{
			sum += *(NameList[i].py+j); 
		}
		NameList[i].sum = sum;
	}
}

void create_hash_table()
{
	memset(HashList, 0, sizeof(HashList));
	for (int i=0; i<NAME_NO; i++)
	{	
		int index = NameList[i].sum%50;
		
		if (HashList[index].si == 0)
		{
			HashList[index].py = NameList[i].py;
			HashList[index].sum = NameList[i].sum;
			HashList[index].si = 1;
		}
		else
		{
			int count = 1;
			do
			{	
				index = (index + NameList[i].sum%10+1)%50;
				count++;
			}while(HashList[index].si != 0);
			HashList[index].py = NameList[i].py;
			HashList[index].sum = NameList[i].sum;
			HashList[index].si = count;
		}
	}
}

void print_hash_table()
{
	cout << "No     " << "Name     "<<"Si    " << endl;
	for(int i=0; i<TABLE_SIZE; i++)
	{	
		cout << i << "     " << (HashList[i].py==NULL?"0":HashList[i].py) << "    " << HashList[i].si << endl;
	}
}

void find_name(const char *name)
{
	if (NULL == name)
		return;

	int sum = 0;
	for(int i=0; i<strlen(name); i++)
		sum += *(name+i);
	int index = sum%50;
	if (HashList[index].si == 1)
	{
		cout << "find: " << HashList[index].py << "in index: " << index << "si: " << HashList[index].si << endl;
	}	
	else if (HashList[index].si == 0)
	{
		cout << "not found: " << name << "in hash table" << endl;
	}
	else
	{
		int count = 1;
		do 
		{
			index = (index + sum%10+1)%50;
			count++;
				
		}while((HashList[index].si==count) || (count > 1000));
		cout << "find: " << HashList[index].py << "in index: " << index << "si: " << HashList[index].si << endl;
	}
	return;
}

int main()
{
	init_name_table();
	create_hash_table();
	print_hash_table();	

	string name;
	cout << "input the name you want find: ";
	while(cin>>name)
	{
		find_name(name.c_str());
		cout << "input the name you want find: ";
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值