C语言哈希表

Python022

C语言哈希表,第1张

/#include "iostream.h"

#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

cin>>temp->name

cout<<"输入地址:"<<endl

cin>>temp->address

cout<<"输入电话:"<<endl

cin>>temp->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=0i<20i++)

{

phone[i]=new node

phone[i]->next=NULL

}

}

void create2() //新建节点

{

int i

nam=new mingzi[20]

for(i=0i<20i++)

{

nam[i]=new node

nam[i]->next=NULL

}

}

void list() //显示列表

{

int i

node *p

for(i=0i<20i++)

{

p=phone[i]->next

while(p)

{

cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl

p=p->next

}

}

}

void list2() //显示列表

{

int i

node *p

for(i=0i<20i++)

{

p=nam[i]->next

while(p)

{

cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl

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)

cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl

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)

cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl

else cout<<"无此记录"<<endl

}

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

{

int i

node *p

for(i=0i<20i++)

{

p=phone[i]->next

while(p)

{

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

iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl

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()

cin>>sel

if(sel==3)

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

int b

cin>>b

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 语言代码转换为 Python 语言可能需要对哈希表和其他数据结构进行重新实现。但是可以提供一个类似的实现方式

def search_hash(hash_table, name):

collisions = 0 # to keep track of number of collisions

index = hash_function(name)

while hash_table[index] is not None and hash_table[index]['name'] != name:

collisions += 1

index = collision_resolution(index)

if hash_table[index] is not None:

print("Search successful! Number of collisions:", collisions)

print("Name: ", hash_table[index]['name'])

print("ID: ", hash_table[index]['id'])

print("Phone: ", hash_table[index]['phone'])

else:

print("Search unsuccessful.")

这个例子使用了字典来存储联系人的信息,其中 'name','id' 和 'phone' 是字典的键。hash_function() 和 collision_resolution() 函数可以用 Python 中的内置函数来实现,或者自己实现。

注意,这只是一种类似的实现方式,并不能完全替代原来的代码,还需要根据实际需求进行修改。

另外,在 Python 中可以使用字典或字典组成的列表来存储哈希表,可以使用字典中的 get() 方法或者列表中的 in 关键字来查找一个元素是否在字典或列表中,如果要实现类似 C 语言中的冲突解决方式,可以在字典中使用链表或线性探测法来实现。

这里只是给出了一种可能的实现方式,具体实现还需要根据具体需求进行调整。

1.选择合适的哈希函数H(key)=key%p(p为小于或等于哈希表长的最大质数);

2.计算各个关键字的直接哈希函数值;

3.根据处理冲突的方法建立哈希表,并输出;

4.在哈希表中进行查找,输出查找的结果,以及所需和记录关键字比较的次数,并计算和输出在等概率情况下查找成功的平均查找长度。

#include<stdlib.h>

#include<stdio.h>

#define NULL 0

typedef int KeyType

typedef struct

{

KeyType key

} ElemType

int haxi(KeyType key,int m)/*根据哈希表长m,构造除留余数法的哈希函数haxi*/

{

int i,p,flag

for(p=mp>=2p--)/*p为不超过m的最大素数*/

{

for(i=2,flag=1i<=p/2&&flagi++)

if(p%i==0)

flag=0

if(flag==1)

break

}

return key%p /*哈希函数*/

}

void inputdata(ElemType **ST,int n)/*从键盘输入n个数据,存入数据表ST(采用动态分配的数组空间)*/

{

KeyType key

int i

(*ST)=(ElemType*)malloc(n*sizeof(ElemType))

printf("\nPlease input %d data:",n)

for(i=1i<=ni++)

scanf("%d",&((*ST)[i].key))

}

void createhaxi(ElemType **HASH,ElemType *ST,int n,int m)/*由数据表ST构造哈希表HASH,n、m分别为数据集合ST和哈希表的长度*/

{

int i,j

(*HASH)=(ElemType *)malloc(m*sizeof(ElemType))

for(i=0i<mi++)

(*HASH)[i].key=NULL /*初始化哈希为空表(以0表示空)*/

for(i=0i<ni++)

{

j=haxi(ST[i].key,m) /*获得直接哈希地址*/

while((*HASH)[j].key!=NULL)

j=(j+1)%m /*用线性探测再散列技术确定存放位置*/

(*HASH)[j].key=ST[i].key /*将元素存入哈希表的相应位置*/

}

}

int search(ElemType *HASH,KeyType key,int m,int *time)/*在表长为m的哈希表中查找关键字等于key的元素,并用time记录比较次数,若查找成功,函数返回值为其在哈希表中的位置,否则返回-1*/

{

int i

*time=1

i=haxi(key,m)

while(HASH[i].key!=0&&HASH[i].key!=key)

{

i++

++*time

}

if(HASH[i].key==0)

return -1

else

return i

}

main()

{

ElemType *ST,*HASH

KeyType key

int i,n,m,stime,time

char ch

printf("\nPlease input n&&m(n<=m)") /*输入关键字集合元素个数和哈希表长*/

scanf("%d%d",&n,&m)

inputdata(&ST,n) /*输入n个数据*/

createhaxi(&HASH,ST,n,m) /*创建哈希表*/

printf("\nThe haxi Table is\n") /*输出已建立的哈希表*/

for(i=0i<mi++)

printf("%5d",i)

printf("\n")

for(i=0i<mi++)

printf("%5d",HASH[i].key)

do/*哈希表的查找,可进行多次查找*/

{

printf("\nInput the key you want to search:")

scanf("%d",&key)

i=search(HASH,key,m,&time)

if(i!=-1)/*查找成功*/

{

printf("\nSuccess,the position is %d",i)

printf("\nThe time of compare is %d",time)

}

else/*查找失败*/

{

printf("\nUnsuccess")

printf("\nThe time of compare is %d",time)

}

printf("\nContiue(y/n):\n") /*是否继续查找yes or no*/

ch=getch()

}

while(ch=='y'||ch=='Y') /*计算表在等概率情况下的平均查找长度,并输出*/

for(stime=0,i=0i<ni++)

{

search(HASH,ST[i].key,m,&time)

stime+=time

}

printf("\nThe Average Search Length is %5.2f",(float)stime/n)

ch=getch()

}