c語言哈希字典,c語言實現哈希

本文目錄一覽:

C語言實現哈希表的相關運算演算法 編寫程序實現哈希表的構造過程。

#define MaxSize 100 //定義最大哈希表長度

#define NULLKEY -1 //定義空關鍵字值

#define DELKEY -2 //定義被刪關鍵字值

typedef int KeyType; //關鍵字類型

typedef char * InfoType; //其他數據類型

typedef struct

{

KeyType key; //關鍵字域

InfoType data; //其他數據域

int count; //探查次數域

} HashData;

typedef HashData HashTable[MaxSize]; //哈希表類型

void InsertHT(HashTable ha,int n,KeyType k,int p) //將關鍵字k插入到哈希表中

{

int i,adr;

adr=k % p;

if (ha[adr].key==NULLKEY || ha[adr].key==DELKEY) //x[j]可以直接放在哈希表中

{

ha[adr].key=k;

ha[adr].count=1;

}

else //發生衝突時採用線性探查法解決衝突

{

i=1; //i記錄x[j]發生衝突的次數

do

{

adr=(adr+1) % p;

i++;

}

while (ha[adr].key!=NULLKEY ha[adr].key!=DELKEY);

ha[adr].key=k;

ha[adr].count=i;

}

n++;

}

void CreateHT(HashTable ha,KeyType x[],int n,

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語言代碼如何轉換成Python語言?(關於哈希表)

def search_hash1(H, c):

str = input(“\n請輸入要查找記錄的姓名:\n”)

p = hash1(str)

pp = p

while H[pp] is not None and eq(str, H[pp].name) == -1:

pp = collision(p, c)

if H[pp] is not None and eq(str, H[pp].name) == 1:

print(f”\n查找成功!\n查找過程衝突次數為{c}.以下是您需要要查找的信息:\n\n姓名:{H[pp].name}\n學號:{H[pp].xuehao}\n電話號碼:{H[pp].tel}”)

else:

print(“\n此人不存在,查找不成功!”)

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;

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;

}

原創文章,作者:NRPX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/132830.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
NRPX的頭像NRPX
上一篇 2024-10-03 23:54
下一篇 2024-10-03 23:54

相關推薦

  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • Python中取出字典中對應鍵的值

    如何使用Python在字典中獲取特定鍵的值?這是Python編程中必須掌握的技能之一。本文將通過多個方面來詳細講解Python如何取出字典中對應鍵的值。 一、通過鍵名獲取值 當我們…

    編程 2025-04-29
  • Python如何遍歷字典中的key和value

    本文將詳細講解Python中如何遍歷字典中的key和value,包括多種遍歷方式以及在遍歷過程中的一些應用場景。 一、遍歷字典中的key和value 在Python中,字典是一種無…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python字典列表去重

    這篇文章將介紹如何使用Python對字典列表進行去重操作,並且從多個方面進行詳細的闡述。 一、基本操作 首先我們需要了解Python字典列表去重的基本操作。Python中提供了一種…

    編程 2025-04-28

發表回復

登錄後才能評論