
如何根据数据建立哈希表
#include#include#include#include #include#include#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define SUCCESS 1#define UNSUCCESS 0#define DUPLICATE -1#define NULLKEY 0 \\\/\\\/ 0为无记录标志#define N 10 \\\/\\\/ 数据元素个数#define EQ(a,b) ((a)==(b))#define LT(a,b) ((a)<(b))#define LQ(a,b) ((a)<=(b))typedef int Status; \\\/\\\/ Status是函数的类型,其值是函数结果状态代码,如OK等typedef int Boolean; \\\/\\\/ Boolean是布尔类型,其值是TRUE或FALSEtypedef int KeyType; \\\/\\\/ 设关键字域为整型struct ElemType \\\/\\\/ 数据元素类型{ KeyType key; int ord;};int hashsize[]={11,19,29,37}; \\\/\\\/ 哈希表容量递增表,一个合适的素数序列int m=0; \\\/\\\/ 哈希表表长,全局变量struct HashTable{ ElemType *elem; \\\/\\\/ 数据元素存储基址,动态分配数组 int count; \\\/\\\/ 当前数据元素个数 int sizeindex; \\\/\\\/ hashsize[sizeindex]为当前容量};Status InitHashTable(HashTable &H)\\\/\\\/ 操作结果: 构造一个空的哈希表{ int i; H.count=0; \\\/\\\/ 当前元素个数为0 H.sizeindex=0; \\\/\\\/ 初始存储容量为hashsize[0] m=hashsize[0]; H.elem=(ElemType*)malloc(m*sizeof(ElemType)); if(!H.elem) exit(OVERFLOW); \\\/\\\/ 存储分配失败 for(i=0;i操作结果: 销毁哈希表H{ free(H.elem); H.elem=NULL; H.count=0; H.sizeindex=0;}unsigned Hash(KeyType K)\\\/\\\/ 一个简单的哈希函数(m为表长,全局变量){ return K%m;}void collision(int &p,int d) \\\/\\\/ 线性探测再散列{ p=(p+d)%m;\\\/\\\/ 开放定址法处理冲突}Status SearchHash(HashTable H,KeyType K,int &p,int &c)\\\/\\\/ 在开放定址哈希表H中查找关键码为K的元素,若查找成功,以p指示待查数据{ p=Hash(K); \\\/\\\/ 求得哈希地址 while(H.elem[p].key!=NULLKEY&&!EQ(K,H.elem[p].key)) { \\\/\\\/ 该位置中填有记录.并且关键字不相等 c++; if(ckey!=NULLKEY) \\\/\\\/ 该单元有数据 *p++=*(H.elem+i); H.count=0; H.sizeindex++; \\\/\\\/ 增大存储容量 m=hashsize[H.sizeindex]; p=(ElemType*)realloc(H.elem,m*sizeof(ElemType)); if(!p) exit(OVERFLOW); \\\/\\\/ 存储分配失败
用java编写哈希表,输入一组数据,创建一个哈希表,然后进行元素的查询、删除
简单的理解为key是value的代号,比如 HashMap map = new HashMap();map.put(小王,王尼玛);map.put(小黄,黄世仁);小王就是王尼玛的key ,王尼玛 就是value本身,map.get(小王) 得到的就是 王尼玛;
构建哈希表,原始数据需不需要先进行排序
不需要.哈希表应该是对数字本身信息进行处理加工使得其信息容易判断是否存在,而不是处理的数字之间的顺序关系而使得容易判断.如果拍了序,那干脆二分查找得了...
如何建立哈希表
C++题目
#include #include #include \\\/\\\/#define LEN 6#define INVIALD -1#define INVAL 1typedef struct node node_t;struct node { char ch[10]; int flag; node_t* next;};typedef struct hash hash_t;struct hash { node_t* arr;};void inithash(hash_t* ha,int len){ ha->arr = (node_t*)calloc(len,sizeof(node_t)); int i; for (i = 0; i < len; i++) ha->arr[i].flag = INVIALD;}int func(const char* str){ int ret = 0; while (0 != *str) ret += *str++; return ret % LEN;}void add(hash_t* ha,const char* str){ int position = func(str); if (INVIALD == ha->arr[position].flag){ strcpy(ha->arr[position].ch,str); ha->arr[position].flag = INVAL; } else { node_t* nd = (node_t*)malloc(sizeof(node_t)); strcpy(nd->ch,str); nd->next = NULL; node_t* index = &(ha->arr[position]); nd->next = index->next; index->next = nd; ha->arr[position].flag = INVAL; }}node_t* search(hash_t* ha,int len,const char* str){ int ret = func(str); if (0 == strcmp(ha->arr[ret].ch,str)) return &(ha->arr[ret]); node_t* index = &(ha->arr[ret]); while (index){ if (0 == strcmp(index->ch,str)) return index; index = index->next; } return NULL;}int main(){ hash_t* ha = (hash_t*)malloc(sizeof(hash_t)); ha->arr = NULL; inithash(ha,LEN); int i; for (i = 0; i < LEN; i++){ char name[10] = {0}; sprintf(name,haha%d,i+1); add(ha,name); } add(ha,ahah3); node_t* nd = search(ha,LEN,ahah3); printf(%s %d\\\ ,nd->ch,nd->flag); return 0;}
map 和 treemap的区别
答案确定是1, 刚刚看了下2013数据结构高分笔记 ,由 装填因子 a 的定义知道,a=n\\\/m 其中n 为关键字个数,m为表长。
具体在264页。