c语言hash函数 c语言hash用法

C语言哈希表

/#include "iostream.h"

创新互联是专业的茶陵网站建设公司,茶陵接单;提供成都网站建设、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行茶陵网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

#include iostream

#include "string.h"

#include "fstream"

#define NULL 0

unsigned int key;

unsigned int key2;

int *p;

struct node //建节点

{

char name[8],address[20];

char num[11];

node * next;

};

typedef node* pnode;

typedef node* mingzi;

node **phone;

node **nam;

node *a;

using namespace std; //使用名称空间

void hash(char num[11]) //哈希函数

{

int i = 3;

key=(int)num[2];

while(num[i]!=NULL)

{

key+=(int)num[i];

i++;

}

key=key%20;

}

void hash2(char name[8]) //哈希函数

{

int i = 1;

key2=(int)name[0];

while(name[i]!=NULL)

{

key2+=(int)name[i];

i++;

}

key2=key2%20;

}

node* input() //输入节点

{

node *temp;

temp = new node;

temp-next=NULL;

cout"输入姓名:"endl;

cintemp-name;

cout"输入地址:"endl;

cintemp-address;

cout"输入电话:"endl;

cintemp-num;

return temp;

}

int apend() //添加节点

{

node *newphone;

node *newname;

newphone=input();

newname=newphone;

newphone-next=NULL;

newname-next=NULL;

hash(newphone-num);

hash2(newname-name);

newphone-next = phone[key]-next;

phone[key]-next=newphone;

newname-next = nam[key2]-next;

nam[key2]-next=newname;

return 0;

}

void create() //新建节点

{

int i;

phone=new pnode[20];

for(i=0;i20;i++)

{

phone[i]=new node;

phone[i]-next=NULL;

}

}

void create2() //新建节点

{

int i;

nam=new mingzi[20];

for(i=0;i20;i++)

{

nam[i]=new node;

nam[i]-next=NULL;

}

}

void list() //显示列表

{

int i;

node *p;

for(i=0;i20;i++)

{

p=phone[i]-next;

while(p)

{

coutp-name'_'p-address'_'p-numendl;

p=p-next;

}

}

}

void list2() //显示列表

{

int i;

node *p;

for(i=0;i20;i++)

{

p=nam[i]-next;

while(p)

{

coutp-name'_'p-address'_'p-numendl;

p=p-next;

}

}

}

void find(char num[11]) //查找用户信息

{

hash(num);

node *q=phone[key]-next;

while(q!= NULL)

{

if(strcmp(num,q-num)==0)

break;

q=q-next;

}

if(q)

coutq-name"_" q-address"_"q-numendl;

else cout"无此记录"endl;

}

void find2(char name[8]) //查找用户信息

{

hash2(name);

node *q=nam[key2]-next;

while(q!= NULL)

{

if(strcmp(name,q-name)==0)

break;

q=q-next;

}

if(q)

coutq-name"_" q-address"_"q-numendl;

else cout"无此记录"endl;

}

void save() //保存用户信息

{

int i;

node *p;

for(i=0;i20;i++)

{

p=phone[i]-next;

while(p)

{

fstream iiout("out.txt", ios::out);

iioutp-name"_"p-address"_"p-numendl;

p=p-next;

}

}

}

void menu() //菜单

{

cout"0.添加记录"endl;

cout"3.查找记录"endl;

cout"2.姓名散列"endl;

cout"4.号码散列"endl;

cout"5.清空记录"endl;

cout"6.保存记录"endl;

cout"7.退出系统"endl;

}

int main()

{

char num[11];

char name[8];

create();

create2() ;

int sel;

while(1)

{

menu();

cinsel;

if(sel==3)

{ cout"9号码查询,8姓名查询"endl;

int b;

cinb;

if(b==9)

{ cout"请输入电话号码:"endl;

cin num;

cout"输出查找的信息:"endl;

find(num);

}

else

{ cout"请输入姓名:"endl;

cin name;

cout"输出查找的信息:"endl;

find2(name);}

}

if(sel==2)

{ cout"姓名散列结果:"endl;

list2();

}

if(sel==0)

{ cout"请输入要添加的内容:"endl;

apend();

}

if(sel==4)

{ cout"号码散列结果:"endl;

list();

}

if(sel==5)

{ cout"列表已清空:"endl;

create();

create2();

}

if(sel==6)

{ cout"通信录已保存:"endl;

save();

}

if(sel==7) return 0;

}

return 0;

}

C语言版数据结构哈希算法题:设m=16,HASH函数为H(key)=key mod 13,现采用再哈希法Hi=RHi(key)处理冲突

应该是这个意思:

第一次冲突就是散列的位置+1,这次发生冲突了就继续第二次

第二次用的是平方取中,55^2= 3025,当然第二次冲突的RH2就是02了,答案(2)

C语言编程,求字符串的hash值(散列值)

#includestdio.h

int main(){

char s[256];

char *p;

unsigned long long int h = 0;

scanf("%s", s);

for(p=s; *p; p++){

h = h*31 + *p;

}

printf("%llu", h);

}

本文标题:c语言hash函数 c语言hash用法
转载注明:https://www.cdcxhl.com/article18/doghpdp.html

成都网站建设公司_创新互联,为您提供域名注册网站维护企业建站网站营销网站收录

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作