




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、C語言實現哈希表(key為字符型)簡單實現了哈希表的插入和查找功能,簡要說明如下:1、數據結構:structHashNodechar*sKey;鍵/intnValue;值/HashNode*pNext;當Hash值沖突時,指向HASH值相同的下一個節點。HashNode*hashTableHASH_TABLE_MAX_SIZE;/哈希表的數組inthash_table_size;/哈希表中元素的個數2、函數:voidhash_table_init()初始化哈希表voidhash_table_insert(constchar*skey,intnvalue)向哈希表中插入鍵位skey,值為nval
2、ue的鍵值對。當skey已經在哈希表中時,忽略該鍵值對。voidhash_table_remove(constchar*skey)從哈希表中刪除鍵值對。HashNode*hash_table_lookup(constchar*skey)查找鍵值為skey的節點。當找到時,返回對應的HashNode指針,沒有找到時,返回NULL。voidhash_table_release()釋放哈希表的內存空間。C語言實現的哈希表(HashTable)源碼如下:01./*02.*Author:puresky03.*Date:2011/01/0804.*Purpose:asimpleimplementation
3、ofHashTableinC05.*/06.07.#include08.#include09.#include10. #include11.11. /*=hashtablestart=*/13.12. #defineHASH_TABLE_MAX_SIZE1000013. typedefstructHashNode_StructHashNode;16.14. structHashNode_Struct15. 16. char*sKey;17. intnValue;18. HashNode*pNext;19. ;23.24. HashNode*hashTableHASH_TABLE_MAX_SIZ
4、E;/hashtabledatastrcutrue25. inthash_table_size;/thenumberofkey-valuepairsinthehashtable!26.26. /initializehashtable27. voidhash_table_init()28. 29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80
5、.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.hash_table_size=0;memset(hashTable,0,sizeof(HashNode*)*HASH_TABLE_MAX_SIZE);/stringhashfunctionunsignedinthash_table_hash_str(constchar*skey)constsignedchar*p=(constsignedchar*)skey;unsignedinth=*p;if(h)for
6、(p+=1;*p!=0;+p)h=(h=HASH_TABLE_MAX_SIZE)printf(outofhashtablememory!n);return;unsignedintpos=hash_table_hash_str(skey)%HASH_TABLE_MAX_SIZE;HashNode*pHead=hashTablepos;while(pHead)if(strcmp(pHead-sKey,skey)=0)printf(%salreadyexists!n,skey);return;pHead=pHead-pNext;HashNode*pNewNode=(HashNode*)malloc(
7、sizeof(HashNode);memset(pNewNode,0,sizeof(HashNode);pNewNode-sKey=(char*)malloc(sizeof(char)*(strlen(skey)+1);strcpy(pNewNode-sKey,skey);pNewNode-nValue=nvalue;pNewNode-pNext=hashTablepos;hashTablepos=pNewNode;hash_table_size+;/removekey-valuefromethehashtablevoidhash_table_remove(constchar*skey)uns
8、ignedintpos=hash_table_hash_str(skey)%HASH_TABLE_MAX_SIZE;if(hashTablepos)HashNode*pHead=hashTablepos;HashNode*pLast=NULL;HashNode*pRemove=NULL;while(pHead)if(strcmp(skey,pHead-sKey)=0)pRemove=pHead;break;pLast=pHead;pHead=pHead-pNext;if(pRemove)if(pLast)pLast-pNext=pRemove-pNext;elsehashTablepos=NU
9、LL;free(pRemove-sKey);108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.170.171.172.173.174.175.176.17
10、7.178.179.180.181.182.183.184.185.186.187.188.free(pRemove-sKey);free(pRemove);/lookupakeyinthehashtableHashNode*hash_table_lookup(constchar*skey)unsignedintpos=hash_table_hash_str(skey)%HASH_TABLE_MAX_SIZE;if(hashTablepos)HashNode*pHead=hashTablepos;while(pHead)if(strcmp(skey,pHead-sKey)=0)returnpH
11、ead;pHead=pHead-pNext;returnNULL;/printthecontentinthehashtablevoidhash_table_print()printf(=contentofhashtable=n);inti;for(i=0;i,i);while(pHead)printf(%s:%d,pHead-sKey,pHead-nValue);pHead=pHead-pNext;printf(n);/freethememoryofthehashtablevoidhash_table_release()inti;for(i=0;ipNext;if(pTemp)free(pTe
12、mp-sKey);free(pTemp);/*=hashtableend=*/*=testfunction=*/#defineMAX_STR_LEN20#defineMIN_STR_LEN10voidrand_str(charr)inti;intlen=MIN_STR_LEN+rand()%(MAX_STR_LEN-MIN_STR_LEN);for(i=0;inValue);pNode=hash_table_lookup(key2);printf(lookupresult:%dn,pNode-nValue);printf(nremovetestingn);printf(beforeremove%s:n,key3);hash_table_print();hash_table_remove(key3);printf(afterremove:n);hash_table_print();hash_ta
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025職員雇傭合同協議
- 教育與科技結合的資源管理研究
- 2025一手房的購房合同
- 2025鄉村民宿格格子店租賃合同
- 2025工程合作公司合同模板
- 2025物流企業勞動合同模板
- T/SHBX 006-2021全生物降解垃圾袋
- 2025最權威的房屋裝修合同范本
- 企業組織文化與數字化激勵體系的融合策略探討
- 成人教育在應對職場變革中的作用
- 租賃電瓶合同范文
- 《石油化工儲運系統罐區設計規范》(SHT3007-2014)
- 安徽省江南十校2023-2024學年高二下學期5月階段聯考化學A試題
- 第六單元 資本主義制度的初步確立 復習課件 2024-2025學年統編版九年級歷史上冊
- 弘揚偉大長征精神-走好今天的長征路課件
- 雙減背景下初中數學分層設計作業課題研究結題總結匯報
- 老媽是個菜販子(2022年海南中考語文試卷記敘文閱讀題及答案)
- 低空經濟產業園商業計劃
- 四川省綿陽市游仙區2024-2025學年高二語文上學期期末考試考試試題
- 別墅清包工合同協議書
- DL∕T 1297-2013 電能質量監測系統技術規范
評論
0/150
提交評論