C语言哈希表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言哈希表相关的知识,希望对你有一定的参考价值。
//功能,利用哈希表制作电话查询系统,根据姓名查询电话
//建立了哈希表之后,我想打印出这个表,但是没输出结果
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#define MAXSIZE 5
#define namelength 20
#define phonelength 15
typedef struct
char name[namelength];
char phone[phonelength];
Person[MAXSIZE];
typedef struct
Person per;
int number;
int sign[MAXSIZE];
int size;
Hash;
int D[12]=1,2,3,4,5,6,7,8,9,10,11,12;
void Create_Hash(char* File_name,Hash &H)
//File_name表示的是文件名字
void InsertHash_Name(Hash &H,char name[],char phone[]);
ifstream in;
char name[namelength]="";
char phone[phonelength]="";
in.open(File_name);
if(in.fail())
printf("文件打开失败!\n");
exit(1);
while(!in.eof())
char str[100];
strcpy(name,"");
strcpy(phone,"");
in.getline(str,100,'\n');
if(str[0]=='*')
break;//结束文件
for(int i=0;str[i]!=' ';i++)
name[i]=str[i];
while(str[i]==' ')
i++;
for(int j=0;str[i]!=' ';j++,i++)
phone[j] = str[i];
InsertHash_Name(H,name,phone);
delete []str;
in.close();
void InsertHash_Name(Hash &H,char name[namelength],char phone[phonelength])
int collision(Hash H,int key,int i);
int hash(char name[namelength]);
int i=0; //记录冲突的次数
int key = hash(name);
while(H.sign[key] == 1)
key=collision(H,key,i++);
if(key == -1)
exit(1);
H.number++;
strcpy(H.per[key].name,name);
strcpy(H.per[key].phone,phone);
H.sign[key] = 1;
//处理冲突
int collision(Hash H,int key,int i)
int x;
if(H.sign[key])
x=(key+D[i]) % MAXSIZE;
return x;
return -1;
int hash(char name[namelength])
int h;
int temp;
char *p;
h=0;
for(p =name; *p; p++)
temp = (int)*p;
h = h + temp;
h=h % MAXSIZE;
return h;
void main()
void Create_Hash(char* File_name,Hash &H);
Hash H;
int j,s;
for(j=0;j<MAXSIZE;j++)
H.sign[j] = 0;
H.number = 0;
H.size = MAXSIZE;
char * Filename = "1.txt";
Create_Hash(Filename,H);
for(s = 0;s<H.size;s++)
printf("%s%s\n",H.per[s].name,H.per[s].phone);
#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=0;i<20;i++)
phone[i]=new node;
phone[i]->next=NULL;
void create2() //新建节点
int i;
nam=new mingzi[20];
for(i=0;i<20;i++)
nam[i]=new node;
nam[i]->next=NULL;
void list() //显示列表
int i;
node *p;
for(i=0;i<20;i++)
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=0;i<20;i++)
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=0;i<20;i++)
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语言编写一个哈希表
简介:
本文摘自 jamesroutley/write-a-hash-table
文章 GitHub 地址:https://github.com/jamesroutley/write-a-hash-table
源码 GitHub 原作者:https://github.com/jamesroutley/algorithms-and-data-structures
内容:
以上是关于C语言哈希表的主要内容,如果未能解决你的问题,请参考以下文章