64,281
社区成员
发帖
与我相关
我的任务
分享
int hash(char *key) //hash索引值算法
{
int n = 0;
char *ch = key;
while (*ch)
{
n <<= 1;
n += (*ch);
++ch;
}
return(n % HTMAXSIZE);
}
HTtable *HTcreate() //创建hash table
{
........
}
int HTinsert(HTtable *ht, char* key, HTelem elem) //数据插入hash table,成功返回1,失败返回0
{
.......
}
HTelem HTfind(HTtable *ht, char* key) //根据关键字查找用户数据,找到返回用户数据指针,否则返回空
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE)) return(NOTFOUND);
entry = ht[i];
while (entry != NULL)
{
if (strcmp(entry->elem->key, key) == 0) return(entry->elem);
entry = entry->next;
}
return(NOTFOUND);
}
void HTfree(HTtable *ht) //清空hash table
{
........
}
HTtable* pt;
char *key;
//插入部分
struct HTdata data[] = {
{"100", "VK_UP", "1","1"}, //第一项为主键,代表时间
{"120", "VK_RIGHT","1", "2"},
{"150", "VK_LEFT","1", "3"},
{"170", "VK_LEFT","1", "3"},
{"200", "VK_LEFT","1", "3"}
};
pt = HTcreate();
for(int i=0;i <5; i++)
{
HTinsert(pt, data[i].key, &data[i]);
}
//查找部分
char g[6];
int s;
s = (int)((tNow - tstart)/100);
itoa(s,g,10);
HTelem hs = HTfind(pt, g);
HTfree(pt);
#ifndef __HASH_H
#define __HASH_H
#define HTMAXSIZE 1999
#define NOTFOUND 0
#define MAXLEN 127
#ifdef __cplusplus
extern "C" {
#endif
struct HTdata //时间和按键
{
char *key; //时间,取0.1s为单位大小,将(gettickcount-tstart)/100,在进行强制转换
char *tDown; //按键
char *num1;
char *num2;
};
typedef struct HTdata *HTelem;
struct HTentry //hash table 结构
{
HTelem elem;
struct HTentry *next;
};
typedef struct HTentry *HTtable;
HTtable *HTcreate(void); //创建hash table
int HTinsert(HTtable *, char *, HTelem ); //根据用户提供的关键字和数据插入表格中
HTelem HTfind(HTtable *, char * ); //根据关键字,查找数据
void HTfree(HTtable *); //释放hash table
#ifdef __cplusplus
}
#endif
#endif
#include "stdafx.h"
#include <malloc.h>
#include <string.h>
#include "Hash.h"
int hash(char *key) //hash索引值算法
{
int n = 0;
char *ch = key;
while (*ch)
{
n <<= 1;
n += (*ch);
++ch;
}
return(n % HTMAXSIZE);
}
HTtable *HTcreate() //创建hash table
{
int i;
HTtable *ht =
(HTtable *) calloc(HTMAXSIZE, sizeof(HTtable));
if (ht != NULL)
for (i=0; i<HTMAXSIZE; i++) ht[i] = NULL;
return(ht);
}
int HTinsert(HTtable *ht, char* key, HTelem elem) //数据插入hash table,成功返回1,失败返回0
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE) || (0==elem) ) return(0);
entry = (struct HTentry *) malloc(sizeof(struct HTentry));
if (entry == NULL) return(0);
elem->key = key;
entry->elem = elem;
entry->next = ht[i];
ht[i] = entry;
return(1);
}
HTelem HTfind(HTtable *ht, char* key) //根据关键字查找用户数据,找到返回用户数据指针,否则返回空
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE)) return(NOTFOUND);
entry = ht[i];
while (entry != NULL)
{
//cout<<entry->elem->key<<" "<<key<<endl;
if (strcmp(entry->elem->key, key) == 0) return(entry->elem);
entry = entry->next;
}
return(NOTFOUND);
}
void HTfree(HTtable *ht) //清空hash table
{
struct HTentry *entry,
*nextentry;
int i;
for (i=0; i<HTMAXSIZE; i++)
{
entry = ht[i];
while (entry != NULL)
{
nextentry = entry->next;
free(entry);
entry = nextentry;
}
}
free(ht);
}