C語言數(shù)據(jù)結構哈希表詳解
更新時間:2022年02月26日 15:17:55 作者:橙子@C
哈希表是一種根據(jù)關鍵碼去尋找值的數(shù)據(jù)映射結構,該結構通過把關鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點復雜,我想我舉個例子你就會明白了,最典型的的例子就是字典
/* * 程序名:hash.c,此程序演示哈希表的實現(xiàn),數(shù)據(jù)元素單鏈表帶頭結點。 * */ #include <stdio.h> #include <stdlib.h> #include <string.h> // 哈希表中數(shù)據(jù)元素的結構體。 typedef struct Element { unsigned int key; // 關鍵字。 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ù)元素單鏈表的頭結點。 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; // 對表長取余。 } // 在哈希表中查找關鍵字,成功返回單鏈表結點的地址,失敗返回空。 Node *LookUp(HashTable *hh,unsigned int key) { int ii; ii=Hash(hh,key); // 獲取關鍵key的哈希地址。 Node *pp=hh->head[ii].next; // 遍歷單鏈表。 while( (pp!=NULL) && (pp->elem.key!=key) ) { pp=pp->next; } return pp; } // 從哈希表中刪除關鍵及其數(shù)據(jù),成功返回1,如果關鍵字不存在返回0。 int Delete(HashTable *hh,unsigned int key) { int ii; ii=Hash(hh,key); // 獲取關鍵key的哈希地址。 Node *pp=&hh->head[ii]; // 遍歷單鏈表,pp指針停留在待刪除關鍵key的前一結點。 while( (pp->next!=NULL) && (pp->next->elem.key!=key) ) { pp=pp->next; } if (pp->next==NULL) return 0; // 查找失敗。 Node *tmp=pp->next; // tmp為將要刪除的結點。 pp->next=pp->next->next; // 寫成p->next=tmp->next更簡潔。 free(tmp); // 釋放結點。 hh->count--; // 表中元素個數(shù)減1。 return 1; } // 向哈希表中插入數(shù)據(jù)元素,成功返回1,如果數(shù)據(jù)元素關鍵字已存在,返回0。 int Insert(HashTable *hh,Element *ee) { // 查找關鍵字是否已存在,如果存在,插入失敗。 Node *pp=LookUp(hh,ee->key); if (pp!=NULL) { printf("關鍵字%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; } } // 釋放全部單鏈表的頭結點數(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ù)元素,關鍵字從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ù)元素,關鍵字從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ù)元素,關鍵字從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); // 刪除哈希表中關鍵字為12的數(shù)據(jù)元素。 printf("count=%d\n",hh->count); PrintTable(hh); // 打印哈希表 // 在哈希表中查找關鍵字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; }
到此這篇關于C語言數(shù)據(jù)結構哈希表詳解的文章就介紹到這了,更多相關C語言 哈希表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++文件IO流及stringstream流讀寫文件和字符串操作詳解
本文詳細介紹C++中的文件IO流和stringstream流的使用方法,包括文件的打開、讀寫操作,以及字符串的輸入輸出、轉換等操作。同時提供實用的示例代碼和技巧,幫助讀者更好地掌握這兩種流的使用2023-04-04C++中實現(xiàn)保存數(shù)據(jù)到CSV文件
這篇文章主要介紹了C++中實現(xiàn)保存數(shù)據(jù)到CSV文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08C++實現(xiàn)簡單的生產(chǎn)者-消費者隊列詳解
這篇文章主要為大家詳細介紹了如何利用C++實現(xiàn)一個簡單的生產(chǎn)者-消費者隊列,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-04-04