7.3.1.Open_Hashing 开散列 分离链

貌似有两天没碰D&A了,USACO也扔一边了

前两天我WM的机子模拟android成功,好一阵兴奋,玩了一两天。

超级终端那里,linux上面常用命令大部分可以用,还带颜色。

3D加速太低,导致taking tom录音不行,小鸟僵尸什么的有点小满

貌似小超频下可以解决,但没米万一烧了就得用原来手机那个SB系统了,桑不起丫

本来说今天继续看的

下午无意中搜索到朱天才的阐述,一下被吸引住,一下午没了……

不过确实有启发,第一次用evernote做了笔记


构造散列地址还算简单

等构造散列链表时就有点小晕。

明知道函数结束时会析构,原地址数据就会被清除,但还是没什么好办法。居然想用地址引用看看有木有好运气

google push函数构造,看到malloc。

矮油…非配内存真是桑不起啊桑不起

初始化已经搞定,search明天写,=_=了

(~﹃~)~zZ了


search_word挺简单,就是标记上done后,才想起来

差点忘了free内存,o(╯□╰)o


/*
 *	7.3.1.Open_Hashing.cpp 
 *	2011/08/04
 *	ArtWalk
 */
/*
 *	2011/08/05
 *	add search_word function
 *	it's done
 */
// I forgot to free() datas

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

#define N 8
#define M 13

typedef char key;
typedef int addr;

struct hashdata {
	key word[20];
	addr hash_addr;
};

struct data {
	key word[20];
	data * next;
};

struct hash {
	data * pdata;
};


float Open_Hashing( key words[N][20], key word[] );
void printhashdata(hashdata hashdatas[]);

bool push( hash & hashs, key word [] );
bool free_hashs( hash hashs[]);
float search_word( hash hashs[], key word[]);

int main(int ac, char** av)
{
	key * word = "SOON";
	key words[N][20] = {"A", "FOOL", "AND", "HIS", "MONEY",
		"ARE", "SOON", "PARTED" };

	printf("%s is in position %.1f\n", word, 
		Open_Hashing( words, word ) );	

	return 0;
}

float Open_Hashing( key words[N][20], key word[] ) {
//	search word in words

	hashdata hashdates[N];
	
	// initial hash address
	int i = 0;
	for ( ; i != N; ++i ) {
		strcpy ( hashdates[i].word, words[i] );

		int j = 0;
		hashdates[i].hash_addr = 0;

		for ( ; words[i][j] != '\0'; ++j ) {
			hashdates[i].hash_addr += ( words[i][j] - 'A' + 1 );
		}
		hashdates[i].hash_addr %= M;
	}

	// test initial hash address
	printhashdata(hashdates);


	//	 initial open hashing
	hash hashs[M];	
	for ( i = 0; i != M; ++i ) {
		hashs[i].pdata = NULL;
	}

	for ( i = 0; i != N; ++i ) {
		push( hashs[hashdates[i].hash_addr], hashdates[i].word );
	}

	// search word
	float f = search_word( hashs, word );

	free_hashs( hashs );

	return f;
}

float search_word(hash hashs[] , key word[]) {

	int key_addr = 0;
	int i = 0;
	for ( ; word[i] != '\0'; ++i ) {
		key_addr += ( word[i] - 'A' + 1 );
		key_addr %=13;
	}

	float point = 0.0;
	data * pdata = hashs[key_addr].pdata;
	while ( pdata != NULL ) {
		if ( !strcmp( pdata->word, word ) ) {
			return key_addr + point;
		}
		pdata = pdata->next;
		point += 0.1;
	}
	
	return -1.0;
}

bool free_hashs( hash hashs[]) {

	int i = 0;
	for ( ; i != M; ++i ) {
		data * pdata = hashs[i].pdata;
		data * p = NULL;
		
		while ( pdata != NULL ) {
			p = pdata->next;
			free(pdata);
			pdata = p;
		}
	}

	return true;
}

bool push( hash & hashs, key word [] ) {
	data * datas;
	datas = (data *)malloc( sizeof(data) );	// I forgot to malloc memory
	strcpy(datas->word, word);	// copy
	datas->next = NULL;
	
	if ( hashs.pdata == NULL ) {
		
		hashs.pdata = datas;	// point to datanote
		
		return true;
	} else {
		data * k = hashs.pdata;
		while ( k->next != NULL ) {	// point to end
			k = k->next;
		}
		
		k->next = datas;
		
		return true;
	}
	
	return false;
}

void printhashdata(hashdata hashdatas[]) {
	int i = 0;
	for ( ; i != N; ++i ) {
		printf("%s %d\n", hashdatas[i].word, hashdatas[i].hash_addr);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值