C語言數(shù)據(jù)結(jié)構(gòu)哈希表詳解
/* * 程序名:hash.c,此程序演示哈希表的實(shí)現(xiàn),數(shù)據(jù)元素單鏈表帶頭結(jié)點(diǎn)。 * */ #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ù)項(xiàng),可以是任意數(shù)據(jù)類型。 // char value[1001]; // 數(shù)據(jù)元素其它數(shù)據(jù)項(xiàng),可以是任意數(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ù)元素存儲基址,動(dòng)態(tài)分配數(shù)組。 int tablesize; // 哈希表當(dāng)前大小,即表長。 int count; // 哈希表中數(shù)據(jù)元素的個(gè)數(shù)。 }HashTable; // 初始化哈希表,tablesize為哈希表的表長,返回哈希表的地址。 HashTable *InitHashTable(const unsigned int tablesize) { // 分配哈希表。 HashTable *hh=(HashTable *)malloc(sizeof(HashTable)); hh->tablesize=tablesize; // 哈希表長。 // 分配和初始化數(shù)據(jù)元素單鏈表的頭結(jié)點(diǎn)。 hh->head=(Node *)malloc((hh->tablesize)*sizeof(Node)); memset(hh->head,0,(hh->tablesize)*sizeof(Node)); hh->count=0; // 哈希表中數(shù)據(jù)元素個(gè)數(shù)置為0。 return hh; } // 哈希函數(shù)。 unsigned int Hash(HashTable *hh,unsigned int key) { return key%hh->tablesize; // 對表長取余。 } // 在哈希表中查找關(guān)鍵字,成功返回單鏈表結(jié)點(diǎn)的地址,失敗返回空。 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é)點(diǎn)。 while( (pp->next!=NULL) && (pp->next->elem.key!=key) ) { pp=pp->next; } if (pp->next==NULL) return 0; // 查找失敗。 Node *tmp=pp->next; // tmp為將要?jiǎng)h除的結(jié)點(diǎn)。 pp->next=pp->next->next; // 寫成p->next=tmp->next更簡潔。 free(tmp); // 釋放結(jié)點(diǎn)。 hh->count--; // 表中元素個(gè)數(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++; // 表中元素個(gè)數(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é)點(diǎn)數(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)文章
C++文件IO流及stringstream流讀寫文件和字符串操作詳解
本文詳細(xì)介紹C++中的文件IO流和stringstream流的使用方法,包括文件的打開、讀寫操作,以及字符串的輸入輸出、轉(zhuǎn)換等操作。同時(shí)提供實(shí)用的示例代碼和技巧,幫助讀者更好地掌握這兩種流的使用2023-04-04c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽多個(gè)端口
這篇文章主要介紹了c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽多個(gè)端口的方法,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下2021-03-03C++中實(shí)現(xiàn)保存數(shù)據(jù)到CSV文件
這篇文章主要介紹了C++中實(shí)現(xiàn)保存數(shù)據(jù)到CSV文件方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08C++中關(guān)于多態(tài)實(shí)現(xiàn)和使用方法
這篇文章主要介紹了C++中關(guān)于多態(tài)實(shí)現(xiàn)和使用方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07C++實(shí)現(xiàn)簡單的生產(chǎn)者-消費(fèi)者隊(duì)列詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)一個(gè)簡單的生產(chǎn)者-消費(fèi)者隊(duì)列,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04