C語言數(shù)據(jù)結(jié)構(gòu)哈希表詳解
更新時間:2022年02月26日 15:17:55 作者:橙子@C
哈希表是一種根據(jù)關(guān)鍵碼去尋找值的數(shù)據(jù)映射結(jié)構(gòu),該結(jié)構(gòu)通過把關(guān)鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點復雜,我想我舉個例子你就會明白了,最典型的的例子就是字典







/*
* 程序名:hash.c,此程序演示哈希表的實現(xiàn),數(shù)據(jù)元素單鏈表帶頭結(jié)點。
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 哈希表中數(shù)據(jù)元素的結(jié)構(gòu)體。
typedef struct Element
{
unsigned int key; // 關(guān)鍵字。
int value; // 數(shù)據(jù)元素其它數(shù)據(jù)項,可以是任意數(shù)據(jù)類型。
// char value[1001]; // 數(shù)據(jù)元素其它數(shù)據(jù)項,可以是任意數(shù)據(jù)類型。
}Element;
// 數(shù)據(jù)元素單鏈表。
typedef struct Node
{
Element elem; // 數(shù)據(jù)元素。
struct Node *next; // next指針。
}Node;
// 哈希表
typedef struct HashTable
{
struct Node *head; // 數(shù)據(jù)元素存儲基址,動態(tài)分配數(shù)組。
int tablesize; // 哈希表當前大小,即表長。
int count; // 哈希表中數(shù)據(jù)元素的個數(shù)。
}HashTable;
// 初始化哈希表,tablesize為哈希表的表長,返回哈希表的地址。
HashTable *InitHashTable(const unsigned int tablesize)
{
// 分配哈希表。
HashTable *hh=(HashTable *)malloc(sizeof(HashTable));
hh->tablesize=tablesize; // 哈希表長。
// 分配和初始化數(shù)據(jù)元素單鏈表的頭結(jié)點。
hh->head=(Node *)malloc((hh->tablesize)*sizeof(Node));
memset(hh->head,0,(hh->tablesize)*sizeof(Node));
hh->count=0; // 哈希表中數(shù)據(jù)元素個數(shù)置為0。
return hh;
}
// 哈希函數(shù)。
unsigned int Hash(HashTable *hh,unsigned int key)
{
return key%hh->tablesize; // 對表長取余。
}
// 在哈希表中查找關(guān)鍵字,成功返回單鏈表結(jié)點的地址,失敗返回空。
Node *LookUp(HashTable *hh,unsigned int key)
{
int ii;
ii=Hash(hh,key); // 獲取關(guān)鍵key的哈希地址。
Node *pp=hh->head[ii].next;
// 遍歷單鏈表。
while( (pp!=NULL) && (pp->elem.key!=key) )
{
pp=pp->next;
}
return pp;
}
// 從哈希表中刪除關(guān)鍵及其數(shù)據(jù),成功返回1,如果關(guān)鍵字不存在返回0。
int Delete(HashTable *hh,unsigned int key)
{
int ii;
ii=Hash(hh,key); // 獲取關(guān)鍵key的哈希地址。
Node *pp=&hh->head[ii];
// 遍歷單鏈表,pp指針停留在待刪除關(guān)鍵key的前一結(jié)點。
while( (pp->next!=NULL) && (pp->next->elem.key!=key) )
{
pp=pp->next;
}
if (pp->next==NULL) return 0; // 查找失敗。
Node *tmp=pp->next; // tmp為將要刪除的結(jié)點。
pp->next=pp->next->next; // 寫成p->next=tmp->next更簡潔。
free(tmp); // 釋放結(jié)點。
hh->count--; // 表中元素個數(shù)減1。
return 1;
}
// 向哈希表中插入數(shù)據(jù)元素,成功返回1,如果數(shù)據(jù)元素關(guān)鍵字已存在,返回0。
int Insert(HashTable *hh,Element *ee)
{
// 查找關(guān)鍵字是否已存在,如果存在,插入失敗。
Node *pp=LookUp(hh,ee->key);
if (pp!=NULL) { printf("關(guān)鍵字%d已存在。\n",ee->key); return 0; }
Node *qq=(Node *)malloc(sizeof(Node));
memcpy(&qq->elem,ee,sizeof(Element));
// 用頭插法插入新數(shù)據(jù)元素。
int ii=Hash(hh,ee->key);
qq->next=hh->head[ii].next;
hh->head[ii].next=qq;
hh->count++; // 表中元素個數(shù)加1。
return 1;
}
// 銷毀哈希表
void FreeHashTable(HashTable *hh)
{
int ii;
Node *pp,*qq;
// 釋放全部的單鏈表。
for(ii=0;ii<hh->tablesize;ii++)
{
pp=hh->head[ii].next;
while(pp)
{
qq=pp->next;
free(pp);
pp=qq;
}
}
// 釋放全部單鏈表的頭結(jié)點數(shù)組。
free(hh->head);
free(hh); // 釋放哈希表。
}
// 打印哈希表。
void PrintTable(HashTable *hh)
{
int ii;
for (ii=0;ii<hh->tablesize;ii++)
{
Node *pp=hh->head[ii].next;
while (pp)
{
printf("[%d-%d] ",pp->elem.key,pp->elem.value);
// printf("[%d-%s] ",pp->elem.key,pp->elem.value);
pp=pp->next;
}
printf("^\n");
}
printf("\n");
}
int main()
{
// 初始化哈希表。
HashTable *hh=InitHashTable(10);
Element ee;
// 插入數(shù)據(jù)元素,關(guān)鍵字從10到20。
ee.key=10; ee.value=110; Insert(hh,&ee);
ee.key=11; ee.value=111; Insert(hh,&ee);
ee.key=12; ee.value=112; Insert(hh,&ee);
ee.key=13; ee.value=113; Insert(hh,&ee);
ee.key=14; ee.value=114; Insert(hh,&ee);
ee.key=15; ee.value=115; Insert(hh,&ee);
ee.key=16; ee.value=116; Insert(hh,&ee);
ee.key=17; ee.value=117; Insert(hh,&ee);
ee.key=18; ee.value=118; Insert(hh,&ee);
ee.key=19; ee.value=119; Insert(hh,&ee);
// 插入數(shù)據(jù)元素,關(guān)鍵字從20到30。
ee.key=20; ee.value=120; Insert(hh,&ee);
ee.key=21; ee.value=121; Insert(hh,&ee);
ee.key=22; ee.value=122; Insert(hh,&ee);
ee.key=23; ee.value=123; Insert(hh,&ee);
ee.key=24; ee.value=124; Insert(hh,&ee);
ee.key=25; ee.value=125; Insert(hh,&ee);
ee.key=26; ee.value=126; Insert(hh,&ee);
ee.key=27; ee.value=127; Insert(hh,&ee);
ee.key=28; ee.value=128; Insert(hh,&ee);
ee.key=29; ee.value=129; Insert(hh,&ee);
// 插入數(shù)據(jù)元素,關(guān)鍵字從30到32。
ee.key=30; ee.value=130; Insert(hh,&ee);
ee.key=31; ee.value=131; Insert(hh,&ee);
ee.key=32; ee.value=132; Insert(hh,&ee);
printf("count=%d\n",hh->count);
PrintTable(hh); // 打印哈希表
Delete(hh,12); // 刪除哈希表中關(guān)鍵字為12的數(shù)據(jù)元素。
printf("count=%d\n",hh->count);
PrintTable(hh); // 打印哈希表
// 在哈希表中查找關(guān)鍵字18。
Node *pp=LookUp(hh,18);
if (pp==0) printf("LookUp(18) failed.\n");
else printf("key=18,value=%d.\n",pp->elem.value);
// ee.key=10; strcpy(ee.value,"<no>00010<no/><name>西施</name><yz>絕世美人</yz>"); Insert(hh,&ee);
// PrintTable(hh); // 打印哈希表
FreeHashTable(hh); // 銷毀哈希表
return 0;
}到此這篇關(guān)于C語言數(shù)據(jù)結(jié)構(gòu)哈希表詳解的文章就介紹到這了,更多相關(guān)C語言 哈希表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VSCODE+cmake配置C++開發(fā)環(huán)境的實現(xiàn)步驟
這篇文章主要介紹了VSCODE+cmake配置C++開發(fā)環(huán)境的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
利用C++?OpenCV?實現(xiàn)從投影圖像恢復仿射特性
我們通過相機拍攝的圖片存在各種畸變,其中投影畸變使得原本平行的直線不再平行,就會產(chǎn)生照片中近大遠小的效果。本文將具體介紹如何利用OPenCV實現(xiàn)從投影圖像恢復仿射特性,接下來跟著小編一起學習吧2021-11-11

