新聞中心
C語言版數據結構哈希算法題:設m=16,HASH函數為H(key)=key mod 13,現采用再哈希法Hi=RHi(key)處理沖突
應該是這個意思:
成都創(chuàng)新互聯公司是專業(yè)的廈門網站建設公司,廈門接單;提供網站設計制作、網站制作,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行廈門網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!
第一次沖突就是散列的位置+1,這次發(fā)生沖突了就繼續(xù)第二次
第二次用的是平方取中,55^2= 3025,當然第二次沖突的RH2就是02了,答案(2)
C語言中的hash函數
Hash,一般翻譯做"散列",也有直接音譯為"哈希"的,就是把任意長度的輸入(又叫做預映射, pre-image),通過散列算法,變換成固定長度的輸出,該輸出就是散列值。這種轉換是一種壓縮映射,也就是,散列值的空間通常遠小于輸入的空間,不同的輸入可能會散列成相同的輸出,而不可能從散列值來唯一的確定輸入值。簡單的說就是一種將任意長度的消息壓縮到某一固定長度的消息摘要的函數。
HASH主要用于信息安全領域中加密算法,它把一些不同長度的信息轉化成雜亂的128位的編碼里,叫做HASH值. 也可以說,hash就是找到一種數據內容和數據存放地址之間的映射關系。Hash算法在信息安全方面的應用主要體現在以下的3個方面:文件校驗、數字簽名、鑒權協議
程程序實現
// 說明:Hash函數(即散列函數)在程序設計中的應用目標 ------ 把一個對象通過某種轉換機制對應到一個
//size_t類型(即unsigned long)的整型值。
// 而應用Hash函數的領域主要是 hash表(應用非常廣)、密碼等領域。
// 實現說明:
// ⑴、這里使用了函數對象以及泛型技術,使得對所有類型的對象(關鍵字)都適用。
// ⑵、常用類型有對應的偏特化,比如string、char*、各種整形等。
// ⑶、版本可擴展,如果你對某種類型有特殊的需要,可以在后面實現專門化。
// ⑷、以下實現一般放在頭文件中,任何包含它的都可使用hash函數對象。
//------------------------------------實現------------------------------------------------
#include string
using std::string;
inlinesize_thash_str(const char* s)
{
unsigned long res = 0;
for (; *s; ++s)
res = 5 * res + *s;
returnsize_t(res);
}
template class Key
struct hash
{
size_toperator () (const Key k) const;
};
// 一般的對象,比如:vector queuestring ;的對象,需要強制轉化
template class Key
size_thashKey::operator () (const Key k) const
{
size_tres = 0;
size_tlen = sizeof(Key);
const char* p = reinterpret_castconst char*(k);
while (len--)
{
res = (res1)^*p++;
}
return res;
}
// 偏特化
template
size_thash string ::operator () (const string str) const
{
return hash_str(str.c_str());
}
typedef char* PChar;
template
size_thashPChar::operator () (const PChar s) const
{
return hash_str(s);
}
typedef const char* PCChar;
template
size_thashPCChar::operator () (const PCChar s) const
{
return hash_str(s);
}
template size_t hashchar::operator () (const char x) const { return x; }
template size_t hashunsigned char::operator () (const unsigned char x) const { return x; }
template size_t hashsigned char::operator () (const signed char x) const { return x; }
template size_t hashshort::operator () (const short x) const { return x; }
template size_t hashunsigned short::operator () (const unsigned short x) const { return x; }
template size_t hashint::operator () (const int x) const { return x; }
template size_t hashunsigned int::operator () (const unsigned int x) const { return x; }
template size_t hashlong::operator () (const long x) const { return x; }
template size_t hashunsigned long::operator () (const unsigned long x) const { return x; }
// 使用說明:
//
// ⑴、使用時首先由于是泛型,所以要加上關鍵字類型。
//
// ⑵、其次要有一個函數對象,可以臨時、局部、全局的,只要在作用域就可以。
//
// ⑶、應用函數對象作用于對應類型的對象。
//----------------------- hash函數使用舉例 -------------------------
#include iostream
#include vector
#include string
using namespace std;
int main()
{
vectorstring vstr⑵;
vstr[0] = "sjw";
vstr[1] = "suninf";
hashstring strhash; // 局部函數對象
cout " Hash value: " strhash(vstr[0]) endl;
cout " Hash value: " strhash(vstr[1]) endl;
cout " Hash value: " hash vectorstring () (vstr) endl;
cout " Hash value: " hashint() (100) endl; // hashint() 臨時函數對象
return 0;
}
c語言hash函數有幾種
#include?stdio.h#include?stdlib.h//這里我自己設計一個hash算法來快速查找一堆數字中相等的數字,這也許是最接近原理的算法了//一個整數整除27后的來作為hash函數//定義一個保存實際數據的結構體節(jié)點struct?data_node{????int?num;????int?count;????struct?data_node?*next;};//定義一個結構體時hash表的一部分typedef?struct{????int?key;?//余數????struct?data_node?*p;?//鏈表的頭指針}?hash_node;#define?HASH_SIZE?27int?do_hash(int?num)?//hash表來求余數,這樣就可以了{????return?num%HASH_SIZE;}//初始化//添加數字//更新數字//刪除數字//查找數字hash_node?HashTable[HASH_SIZE];?//這里申明一個hashtable的數組//初始化函數,需要做的事將key復制為null,將p指針指向null,返回一個頭指針來指向這個hashtablevoid?InitHashTable(hash_node?*HashTable)
{????//進行參數的校驗????for(int?i=0;iHASH_SIZE;i++)
{
HashTable[i].key?=?0;????????HashTable[i].p?=NULL;????}
}//保存到這個鏈表中//如果這個鏈表是空的話,就作為頭指針,如果這個鏈表不為空,則添加到吧數字添加到末尾int?savedata(struct?data_node?**head,int?num)
{????struct?data_node?*tmp_p?=?*head;????struct?data_node?*p?=?(struct?data_node?*)malloc(sizeof(struct?data_node));????if(p?==?NULL)????????return?0;????if(*head?==?NULL)
{
*head?=?p;????????p-count?=?1;????????p-num?=?num;????????p-next?=?NULL;????}????else?//如果不為空,則這個時候應該添加到鏈表末尾????{????????while(tmp_p?!=?NULL)//如果存在,則將這個節(jié)點的count加1就可以了????????{????????????if(tmp_p-num?==?num)
{
free(p);????????????????++tmp_p-count?;????????????????return?0;????????????}????????????if(tmp_p-next?==?NULL)????????????????break;????????????tmp_p?=?tmp_p-next;????????}
tmp_p-next?=?p;????????p-count?=1;????????p-num?=?num;????????p-next?=?NULL;????}????return?0;}//添加數字//將這個數字經過hash求出結果,然后再保存到相應的鏈表中//返回真或者假就可以了int?add_hash(hash_node?*HashTable,int?num)
{????int?mod?=?do_hash(num);????return?savedata(HashTable[mod].p,num);}int?main()
{????int?num?=?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);????//在這里我們可以發(fā)現一個好的hash函數是多么的重要,如果hash函數不好造成很多沖突的話,效率并不會提高很多的,理想的情況是沖突幾乎沒有????//這也就是設計hash函數的精髓所在????return?0;}
數據結構 哈希表,C語言解答
#include stdio.h
#includemalloc.h
#includestring.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="chengdaoquan";
NameList[9].py="ludaoqing";
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;iNAME_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; iHASH_LEN;i++)//哈希表的初始化
{
HashList[i].py="";
HashList[i].k=0;
HashList[i].si=0;
}
for (i=0; iNAME_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;r20;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; i15; 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("按任意鍵繼續(xù)顯示...\n"); //由于數據比較多,所以分屏顯示(以便在Win9x/DOS下能看到所有的數據)
// getch();
for( i=15; i30; 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("按任意鍵繼續(xù)顯示...\n");
// getch();
for( i=30; i40; 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("按任意鍵繼續(xù)顯示...\n");
//getch();
for( i=40; i50; 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;iHASH_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語言編程,求字符串的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語言的嗎,哈希函數是c語言的嗎為什么
網頁鏈接:http://fisionsoft.com.cn/article/hdcpph.html