c語言哈希表查找
① 哈希查找,就是在一個表格里按學生的成績查找,幫忙寫下代碼,用c語言,還有注釋,詳細點,我是菜鳥。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 20
typedef struct
{
int num;
char name[20];
}
ElemType;//定義查找的結點元素
typedef struct
{
ElemType *elem;
int count;
int sizeindex;
}
HashTable;//定義哈希表
int Hash(int num)
{
int p;
p=num%5;
return p;
}//定義哈希函數
void InitHash(HashTable *H)//創建哈希表
{
int i;
H->elem=(ElemType *)malloc(MAX*sizeof(ElemType));
H->count=0;
H->sizeindex=MAX;
for(i=0;i<MAX;i++)
H->elem[i].num=0;//初始化,使SearHash函數能判斷到底有沒有元素在裡面
}
int SearHash(HashTable H,int key,int *p)//查找函數
{
int c=0;
*p=Hash(key);
while(H.elem[*p].num!=key&&H.elem[*p].num!=0)
{//通過二次探測再散列解決沖突
c=c+1;
if(c%2==1)
*p=*p+(c+1)*(c+1)/4;
else
*p=*p-(c*c)/4;
}
if(H.elem[*p].num==key)
return 1;
else
return 0;
}
void InsertHash(HashTable *H,ElemType e)
{//如果查找不到就插入元素
int p;
SearHash(*H,e.num,&p);
H->elem[p]=e;
++H->count;
}
void main()//主函數
{
HashTable H;
int p,key,i;
ElemType e;
InitHash(&H);
for(i=0;i<3;i++)
{//輸入3個元素
printf("輸入學生學號\n");
scanf("%d",&e.num);//輸入學號
if(!SearHash(H,e.num,&p))
{
printf("輸入學生名字\n");
scanf("%s",e.name);//輸入名字
InsertHash(&H,e);//插入元素
}
else
printf("已經存在\n");//否則就表示元素的學好已經存在
}
printf("輸入查找的學生學號:\n");
scanf("%d",&key);//輸入要查找的學號
if(SearHash(H,key,&p))//能查找成功
{
printf("%s\n",H.elem[p].name);//輸出名字
printf("%d\n",p);//輸出位置
}
else
printf(" 不存在");
getch();
}
以前編的,挺簡單的。用二次散列探測解決沖突。除留余數哈希函數法,除數可以任意選,我選的是5。還有哈希表的長度你可以大點。getch();可以不要啊。
② c語言hash函數有幾種
#include<stdio.h>#include<stdlib.h>//這里我自己設計一個hash演算法來快速查找一堆數字中相等的數字,這也許是最接近原理的演算法了//一個整數整除27後的來作為hash函數//定義一個保存實際數據的結構體節點structdata_node{intnum;intcount;structdata_node*next;};//定義一個結構體時hash表的一部分typedefstruct{intkey;//余數structdata_node*p;//鏈表的頭指針}hash_node;#defineHASH_SIZE27intdo_hash(intnum)//hash表來求余數,這樣就可以了{returnnum%HASH_SIZE;}//初始化//添加數字//更新數字//刪除數字//查找數字hash_nodeHashTable[HASH_SIZE];//這里申明一個hashtable的數組//初始化函數,需要做的事將key復制為null,將p指針指向null,返回一個頭指針來指向這個hashtablevoidInitHashTable(hash_node*HashTable)
{//進行參數的校驗for(inti=0;i<HASH_SIZE;i++)
{
HashTable[i].key=0;HashTable[i].p=NULL;}
}//保存到這個鏈表中//如果這個鏈表是空的話,就作為頭指針,如果這個鏈表不為空,則添加到吧數字添加到末尾intsavedata(structdata_node**head,intnum)
{structdata_node*tmp_p=*head;structdata_node*p=(structdata_node*)malloc(sizeof(structdata_node));if(p==NULL)return0;if(*head==NULL)
{
*head=p;p->count=1;p->num=num;p->next=NULL;}else//如果不為空,則這個時候應該添加到鏈表末尾{while(tmp_p!=NULL)//如果存在,則將這個節點的count加1就可以了{if(tmp_p->num==num)
{
free(p);++tmp_p->count;return0;}if(tmp_p->next==NULL)break;tmp_p=tmp_p->next;}
tmp_p->next=p;p->count=1;p->num=num;p->next=NULL;}return0;}//添加數字//將這個數字經過hash求出結果,然後再保存到相應的鏈表中//返回真或者假就可以了intadd_hash(hash_node*HashTable,intnum)
{intmod=do_hash(num);returnsavedata(&HashTable[mod].p,num);}intmain()
{intnum=100;hash_node*H=HashTable;InitHashTable(H);add_hash(H,num);add_hash(H,num);add_hash(H,3);add_hash(H,1);add_hash(H,4);//在這里我們可以發現一個好的hash函數是多麼的重要,如果hash函數不好造成很多沖突的話,效率並不會提高很多的,理想的情況是沖突幾乎沒有//這也就是設計hash函數的精髓所在return0;}
③ 如何用c語言建立哈希表 存有序對
#include<ctype.h>
#include<malloc.h>
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#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或FALSE
typedef 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<m;i++)
H.elem[i].key=NULLKEY; // 未填記錄的標志
return OK;
}
void DestroyHashTable(HashTable &H)// 初始條件: 哈希表H存在。操作結果: 銷毀哈希表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(c<m)
collision(p,c); // 求得下一探查地址p
else
break;
}
if EQ(K,H.elem[p].key)
return SUCCESS; // 查找成功,p返回待查數據元素位置
else
return UNSUCCESS; // 查找不成功(H.elem[p].key==NULLKEY),p返回的是插入位置
}
Status InsertHash(HashTable &,ElemType); // 對函數的聲明
void RecreateHashTable(HashTable &H) // 重建哈希表
{ int i,count=H.count;
ElemType *p,*elem=(ElemType*)malloc(count*sizeof(ElemType));
p=elem;
printf("重建哈希表\n");
for(i=0;i<m;i++) // 保存原有的數據到elem中
if((H.elem+i)->key!=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); // 存儲分配失敗
H.elem=p;
for(i=0;i<m;i++)
H.elem[i].key=NULLKEY; // 未填記錄的標志(初始化)
for(p=elem;p<elem+count;p++) // 將原有的數據按照新的表長插入到重建的哈希表中
InsertHash(H,*p);
}
Status InsertHash(HashTable &H,ElemType e)// 查找不成功時插入數據元素e到開放定址哈希表H中,並返回OK;
{ int c,p;
c=0;
if(SearchHash(H,e.key,p,c)) // 表中已有與e有相同關鍵字的元素
return DUPLICATE;
else if(c<hashsize[H.sizeindex]/2) // 沖突次數c未達到上限,(c的閥值可調)
{ // 插入e
H.elem[p]=e;
++H.count;
return OK;
}
else
RecreateHashTable(H); // 重建哈希表
return ERROR;
}
void TraverseHash(HashTable H,void(*Vi)(int,ElemType))// 按哈希地址的順序遍歷哈希表
{
printf("哈希地址0~%d\n",m-1);
for(int i=0;i<m;i++)
if(H.elem[i].key!=NULLKEY) // 有數據
Vi(i,H.elem[i]);
}
Status Find(HashTable H,KeyType K,int &p)// 在開放定址哈希表H中查找關鍵碼為K的元素,若查找成功,以p指示待查數據
{ int c=0;
p=Hash(K); // 求得哈希地址
while(H.elem[p].key!=NULLKEY&&!EQ(K,H.elem[p].key))// 該位置中填有記錄.並且關鍵字不相等
{ c++;
if(c<m)
collision(p,c); // 求得下一探查地址p
else
return UNSUCCESS; // 查找不成功(H.elem[p].key==NULLKEY)
}
if EQ(K,H.elem[p].key)
return SUCCESS; // 查找成功,p返回待查數據元素位置
else
return UNSUCCESS; // 查找不成功(H.elem[p].key==NULLKEY)
}
void print(int p,ElemType r)//輸出
{
printf("address=%d (%d,%d)\n",p,r.key,r.ord);
}
void main()
{
ElemType r[N]={{17,1},{60,2},{29,3},{38,4},{1,5},{2,6},{3,7},{4,8},{60,9},{13,10}};
HashTable h;
int i,p;
Status j;
KeyType k;
InitHashTable(h);
for(i=0;i<N-1;i++)// 插入前N-1個記錄
{
j=InsertHash(h,r[i]);
if(j==DUPLICATE)
printf("表中已有關鍵字為%d的記錄,無法再插入記錄(%d,%d)\n",r[i].key,r[i].key,r[i].ord);
}
printf("按哈希地址的順序遍歷哈希表:\n");
TraverseHash(h,print);
printf("請輸入待查找記錄的關鍵字: ");
scanf("%d",&k);
j=Find(h,k,p);
if(j==SUCCESS)
print(p,h.elem[p]);
else
printf("沒找到\n");
j=InsertHash(h,r[i]); // 插入第N個記錄
if(j==ERROR) // 重建哈希表
j=InsertHash(h,r[i]); // 重建哈希表後重新插入
printf("按哈希地址的順序遍歷重建後的哈希表:\n");
TraverseHash(h,print);
printf("請輸入待查找記錄的關鍵字: ");
scanf("%d",&k);
j=Find(h,k,p);
if(j==SUCCESS)
print(p,h.elem[p]);
else
printf("沒找到\n");
DestroyHashTable(h);
}
④ 數據結構 哈希表,C語言解答
設計一個哈希表,哈希函數用除留余數法,用開放定址法、線性探測處理沖突,從文本文件讀入30個左右中文人名,每行一人,可用本班學生名單。要求:
1.成功的平均查找長度不超過3,能查找人名,能插入新人名,能顯示當前哈希表中每個元素,每行顯示6個。
2.編寫主函數測試這些功能,並統計實際的平均查找長度。用菜單式操作。
3.如果有餘力,可以增加刪除功能(選做)。
提示:
根據成功的平均查找長度要求和估算公式求出裝填因子α上界及哈希表長度m。
#include <stdio.h>
#include<malloc.h>
#include<string.h>
//#include
#define HASH_LEN 50 //哈希表的長度
#define M 47
#define NAME_NO 30 //人名的個數
typedef struct NAME
{
char *py; //名字的拼音
int k; //拼音所對應的整數
}NAME;
NAME NameList[HASH_LEN];
typedef struct hterm //哈希表
{
char *py; //名字的拼音
int k; //拼音所對應的整數
int si; //查找長度
}HASH;
HASH HashList[HASH_LEN];
/*-----------------------姓名(結構體數組)初始化---------------------------------*/
void InitNameList()
{ int i;
char *f;
int r,s0;
NameList[0].py="chenghongxiu";
NameList[1].py="yuanhao";
NameList[2].py="yangyang";
NameList[3].py="zhanghen";
NameList[4].py="chenghongxiu";
NameList[5].py="xiaokai";
NameList[6].py="liupeng";
NameList[7].py="shenyonghai";
NameList[8].py="chengquan";
NameList[9].py="luqing";
NameList[10].py="gongyunxiang";
NameList[11].py="sunzhenxing";
NameList[12].py="sunrongfei";
NameList[13].py="sunminglong";
NameList[14].py="zhanghao";
NameList[15].py="tianmiao";
NameList[16].py="yaojianzhong";
NameList[17].py="yaojianqing";
NameList[18].py="yaojianhua";
NameList[19].py="yaohaifeng";
NameList[20].py="chengyanhao";
NameList[21].py="yaoqiufeng";
NameList[22].py="qianpengcheng";
NameList[23].py="yaohaifeng";
NameList[24].py="bianyan";
NameList[25].py="linglei";
NameList[26].py="fuzhonghui";
NameList[27].py="huanhaiyan";
NameList[28].py="liudianqin";
NameList[29].py="wangbinnian";
for (i=0;i<NAME_NO;i++)// *求出各個姓名的拼音所對應的整數
{
s0=0;
f=NameList[i].py;
for (r=0;*(f+r) != '\0';r++) //方法:將字元串的各個字元所對應的ASCII碼相加,所得的整數做為哈希表的關鍵字
s0=*(f+r)+s0;
NameList[i].k=s0;
}
}
/*-----------------------建立哈希表---------------------------------*/
void CreateHashList()
{int i; for ( i=0; i<HASH_LEN;i++)//哈希表的初始化 { HashList[i].py=""; HashList[i].k=0; HashList[i].si=0; }
for (i=0; i<NAME_NO;)
{
int sum=0;
int adr=(NameList[i].k) % M; //哈希函數
int d=adr;
if(HashList[adr].si==0) //如果不沖突
{
HashList[adr].k=NameList[i].k;
HashList[adr].py=NameList[i].py;
HashList[adr].si=1;
}
else //沖突
{
do
{
d=(d+((NameList[i].k))%10+1)%M; //偽散列
sum=sum+1; //查找次數加1
}while (HashList[d].k!=0);
HashList[d].k=NameList[i].k;
HashList[d].py=NameList[i].py;
HashList[d].si=sum+1;
}i++;
}
}
/*-------------------------------------查找------------------------------------*/
void FindList()
{ int r;
char name[20]={0};
int s0=0;
int sum=1;
int adr;
int d;
printf("\n\n請輸入姓名的拼音: "); //輸入姓名
scanf("%s",name);
for ( r=0;r<20;r++) //求出姓名的拼音所對應的整數(關鍵字)
s0+=name[r];
adr=s0 % M; //使用哈希函數
d=adr;
if(HashList[adr].k==s0) //分3種情況進行判斷
printf("\n姓名:%s 關鍵字:%d 查找長度為: 1",HashList[d].py,s0);
else if (HashList[adr].k==0)
printf("無該記錄!");
else
{
int g=0;
do
{
d=(d+s0%10+1)%M; //偽散列
sum=sum+1;
if (HashList[d].k==0)
{
printf("無記錄! ");
g=1;
}
if (HashList[d].k==s0)
{
printf("\n姓名:%s 關鍵字:%d 查找長度為:%d",HashList[d].py,s0,sum);
g=1;
}
}while(g==0);
}
}
/*--------------------------------顯示哈希表----------------------------*/
void Display()
{int i; float average=0; printf("\n\n地址\t關鍵字\t\t搜索長度\tH(key)\t\t拼音 \n"); //顯示的格式 for( i=0; i<15; i++) { printf("%d ",i); printf("\t%d ",HashList[i].k); printf("\t\t%d ",HashList[i].si); printf("\t\t%d ",(HashList[i].k)%M); printf("\t %s ",HashList[i].py); printf("\n"); }
// printf("按任意鍵繼續顯示...\n"); //由於數據比較多,所以分屏顯示(以便在Win9x/DOS下能看到所有的數據)
// getch();
for( i=15; i<30; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
// printf("按任意鍵繼續顯示...\n");
// getch();
for( i=30; i<40; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
//printf("按任意鍵繼續顯示...\n");
//getch();
for( i=40; i<50; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
for (i=0;i<HASH_LEN;i++)
{average+=HashList[i].si; average/=NAME_NO; printf("\n\n平均查找長度:ASL(%d)=%f \n\n",NAME_NO,average); }
}
/*--------------------------------主函數----------------------------*/
void main()
{
/* ::SetConsoleTitle("哈希表操作"); //Windows API函數,設置控制台窗口的標題
HANDLE hCon = ::GetStdHandle(STD_OUTPUT_HANDLE); //獲得標准輸出設備的句柄
::SetConsoleTextAttribute(hCon, 10|0); //設置文本顏色
*/
printf("\n------------------------哈希表的建立和查找----------------------");
InitNameList();
CreateHashList ();
while(1)
{ char ch1;
printf("\n\n");
printf(" 1. 顯示哈希表\n");
printf(" 2. 查找\n");
printf(" 3. 退出\n");
err:
scanf("%c",&ch1);
if (ch1=='1')
Display();
else if (ch1=='2')
FindList();
else if (ch1=='3')
return;
else
{
printf("\n請輸入正確的選擇!");
goto err;
}
}
}
⑤ 哈希表設計C++(首選)或C語言——針對你的班級中的人名設計一個哈希表,使得平均查找長度不超過R,完成相
#include<iostream>
#include<string>
using namespace std;
#define HASH_LENGTH 50 //哈希表的長度
#define M 47 //隨機數
#define NAME_NO 30 //人名的個數
typedef struct
{ char *py; //名字的拼音
int k; //拼音所對應的整數
}NAME;
NAME NameList[HASH_LENGTH]; //全局變數NAME
typedef struct //哈希表
{ char *py; //名字的拼音
int k; //拼音所對應的整數
int si; //查找長度
}HASH;
HASH HashList[HASH_LENGTH]; //全局變數HASH
void InitNameList() //姓名(結構體數組)初始化
{ char *f;
int r,s0,i;
for (i=0; i<HASH_LENGTH; i++)//★
{//★
NameList[i].py = new char[64];//★
NameList[i].py[0] = 0;//★
}//★
strcpy(NameList[0].py, "baojie");//★
strcpy(NameList[1].py, "chengaoyang");//★
strcpy(NameList[2].py, "chenguangzhong");//★
strcpy(NameList[3].py, "chenliangliang");//★
strcpy(NameList[4].py, "chenyongzhou");//★
strcpy(NameList[5].py, "fengchao");//★
strcpy(NameList[6].py, "gexiangfeng");//★
strcpy(NameList[7].py, "huting");//★
strcpy(NameList[8].py, "huangpinjin");//★
strcpy(NameList[9].py, "jiangxiaojia");//★
strcpy(NameList[10].py, "laidongjie");//★
strcpy(NameList[11].py, "liyexiao");//★
strcpy(NameList[12].py, "lihui");//★
strcpy(NameList[13].py, "lijue");//★
strcpy(NameList[14].py, "lizhuoqun");//★
strcpy(NameList[15].py, "linfujun");//★
strcpy(NameList[16].py, "luobin");//★
strcpy(NameList[17].py, "luokeqing");//★
strcpy(NameList[18].py, "nichao");//★
strcpy(NameList[19].py, "panhuafeng");//★
strcpy(NameList[20].py, "sijun");//★
strcpy(NameList[21].py, "songzhanhui"); //★
strcpy(NameList[22].py, "sunzhengqing");//★
strcpy(NameList[23].py, "wanghaofeng");//★
strcpy(NameList[24].py, "wangjunshuai");//★
strcpy(NameList[25].py, "wangqinde");//★
strcpy(NameList[26].py, "wangzejun");//★
strcpy(NameList[27].py, "wangkeke");//★
strcpy(NameList[28].py, "weixing");//★
strcpy(NameList[29].py, "wurenke");//★
for(i=0;i<NAME_NO;i++)
{
s0=0;
f=NameList[i].py;
for(r=0;*(f+r)!='\0';r++)
/* 方法:將字元串的各個字元所對應的ASCII碼相加,所得的整數做為哈希表的關鍵字*/
s0=*(f+r)+s0;
NameList[i].k=s0;
}
}
void CreateHashList() //建立哈希表
{
int i;
for(i=0; i<HASH_LENGTH;i++)
{
HashList[i].py=new char[64];//★
HashList[i].py[0] = 0; //★
HashList[i].k=0;
HashList[i].si=0;
}
for(i=0;i<HASH_LENGTH;i++)
{
int sum=0;
int adr=(NameList[i].k)%M;
//哈希函數
int d=adr;
if(HashList[adr].si==0) //如果不沖突
{
HashList[adr].k=NameList[i].k;
HashList[adr].py=NameList[i].py;
HashList[adr].si=1;
}
else //沖突
{
while (HashList[d].k!=0)
{
d=(d+NameList[i].k%10+1)%M; //偽隨機探測再散列法處理沖突
sum=sum+1; //查找次數加1
};
HashList[d].k=NameList[i].k;
HashList[d].py=NameList[i].py;
HashList[d].si=sum+1;
}
}
}
void FindList() //查找
{
string name;
int s0=0,r,sum=1,adr,d;
cout<<"請輸入姓名的拼音:"<<endl;
cin>>name;;
for(r=0;r<20;r++) //求出姓名的拼音所對應的整數(關鍵字)
s0+=name[r];
adr=s0%M; //使用哈希函數
d=adr;
if(HashList[adr].k==s0) //分3種情況進行判斷
cout<<"姓名:"<<HashList[d].py<<" "<<"關鍵字:"<<s0<<" "<<"查找長度為: 1"<<endl;
else if (HashList[adr].k==0)
cout<<"無此記錄!"<<endl;
else
{
int g=0;
while(g==0)
{
d=(d+s0%10+1)%M; //偽隨機探測再散列法處理沖突
sum=sum+1;
if(HashList[d].k==0)
{
cout<<"無此記錄!"<<endl;
g=1;
}
if(HashList[d].k==s0)
{
cout<<"姓名:"<<HashList[d].py<<" "<<"關鍵字:"<<s0<<" "<<"查找長度為:"<<sum<<endl;
g=1;
}
};
}
}
void Display() // 顯示哈希表
{
int i;
float average=0;
cout<<"\n地址\t關鍵字\t\t搜索長度\tH(key)\t 姓名\n"; //顯示的格式
for(i=0; i<50; i++)
{
cout<<i<<" ";
cout<<"\t"<<HashList[i].k<<" ";
cout<<"\t\t"<<HashList[i].si<<" ";
cout<<"\t\t"<<(HashList[i].k%M)<<" ";
cout<<"\t "<<HashList[i].py<<" ";
cout<<"\n";
}
for(i=0;i<HASH_LENGTH;i++)
average+=HashList[i].si;
average/=NAME_NO;
cout<<"平均查找長度:ASL("<<NAME_NO<<")="<<average<<endl;
}
int main()
{
char x;
InitNameList();
CreateHashList ();
cout<<"d. 顯示哈希表 f. 查找 任意鍵退出 請選擇:"<<endl;
while(cin>>x)
{
if(x=='d')
{
Display();
cout<<endl;
}
else if(x=='f')
{
FindList();
cout<<endl;
}
else break;
}
for (int i=0; i<HASH_LENGTH; i++)//★
{
free(NameList[i].py);//★
free(HashList[i].py);//★
}//★
return 0;
}
希望能幫助到你。
⑥ 如何用C語言中實現哈希表
C++有 map,set
還有其他的,看STL相關的吧
數組還慢....
⑦ C語言哈希表,為什麼輸入查找名字就沒了音訊(點進來有詳細內容喲~)
HASH函數里 scanf("%s",&namekey); 去掉namekey前的&號。namekey已經是一個指針了,不需要再取地址。
⑧ C語言哈希表
/#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=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語言設計本班級花名冊的哈希表並提供查找界面。還要有HASH函數和解決沖突的方法
首先 你要明白方法裡面的變數是沒有傳值到外面的
也就是 局部變數的關系
scanf("%f%f%f",&x,&y,&z);
max(x,y,z);
printf("%f",x);
這里你的max 不會傳值到x裡面
你可以這樣
x=max(x,y,z);
printf("%f",x);
或者用指針做形參實現傳值
⑩ 哈希查找演算法程序
查找演算法
基本要求:
(1)設計一個菜單將實現的查找演算法的名字顯示出來,並提示用戶對查找演算法進行選擇;
(2)分別實現順序查找、二分查找(折半查找)、二叉排序樹、哈希查找;
(3)哈希函數採用除留余數發,解決沖突的方法大家任選擇一種;
(4)二叉排序樹必須實現構建、查找、插入、刪除四個基本操作;
(5)輸出各種排序的結果並進行比較。*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
typedef struct /*順序結構數據類型*/
h.length++;
h.r[ }
else
if(k<l.r[mid].key) high=mid-1;
else low=mid +1;
}
if(i!=0)
{
printf("l.r[%d].key=%d\n",i,k);
printf("查找成功\n");
}
return ht;
}
void HashSearch(RecordHash ht) /*哈希查找*/
{
int k,i;
page_title("哈希查找");
printf("請輸入要查找的關鍵字:");
scanf("%d",&k);
i=k%13;
if(ht.HashTable[i].key==k)
{
printf("ht.HashTable[%d].key=%d\n",i,k);
printf("查找成功\n");
}
else
{
i=i+1;
for(;i<MAX;i++)
if(ht.HashTable[i].key==k)
{
printf("ht.HashTable[%d].key=%d\n",i,k);
printf("查找成功\n");
break;
}
if(i==MAX) printf("查找失敗\n");
}
return_confirm();
}
void main()
{
RecordList L1,L2;
BSTNode *pt;
RecordHash ht;
int k,i;
printf("\n創建順序查找線性表,輸入0則結束輸入(可不按順序輸入)\n");
L1=creat1();
printf("\n創建二分查找線性表,輸入0則結束輸入(按遞增順序輸入)\n");
L2=creat1();
printf("\n創建二叉排序樹,輸入0則結束輸入\n");
pt=creat2();
printf("\n創建哈希表\n");
ht=creat3();
menu:page_title("請選擇查找方式,輸入0則結束輸入");
printf("順序查找請按1\n二分查找請按2\n二叉排序樹查找請按3\n哈希查找請按4\n推出請按0\n");
switch(getch())
{
case '1':
SeqSearch(L1);
break;
case '2':
Binsrch(L2);
break;
case '3':
page_title("二叉排序樹查找");
printf("請輸入要查找的關鍵字:");
scanf("%d",&k);
SearchBST(pt,k);
break;
case '4':
HashSearch(ht);
break;
case '0':
exit(0);
default :
printf("輸入錯誤,按任意鍵返回");
getch();
}
goto menu;