首页 > 编程语言 > 详细

(算法)哈希表

时间:2018-04-08 23:28:23      阅读:218      评论:0      收藏:0      [点我收藏+]

  主函数没有代码!!!

1
#include <memory.h> 2 #include <iostream> 3 using namespace std; 4 5 struct MyNode{ 6 int data; 7 MyNode *pNext; 8 }; 9 struct _Hash_Table{ 10 MyNode *value[10]; 11 }; 12 _Hash_Table *create_hash_table(){ 13 _Hash_Table *pHash = new _Hash_Table; 14 memset(pHash, 0, sizeof(_Hash_Table)); 15 return pHash; 16 } 17 18 bool insert_data_into_hash(_Hash_Table *pHash, int data){ 19 if(pHash == NULL) return false; 20 MyNode *pNode; 21 if((pNode = pHash->value[data % 10]) == NULL){ 22 pNode = new MyNode; 23 pNode->data = data; 24 pNode->pNext = NULL; 25 pHash->value[data % 10] = pNode; 26 } 27 else{ 28 while(pNode->pNext){ 29 pNode = pNode->pNext; 30 } 31 pNode->pNext = new MyNode; 32 pNode->pNext->data = data; 33 pNode->pNext->pNext = NULL; 34 } 35 return true; 36 } 37 38 MyNode *find_data_in_hash(_Hash_Table *pHash, int findData){ 39 if(pHash == NULL) return NULL; 40 MyNode *pNode; 41 if((pNode = pHash->value[findData % 10]) == NULL) return NULL; 42 while(pNode){ 43 if(pNode->data == findData) 44 { 45 return pNode; 46 } 47 pNode = pNode->pNext; 48 } 49 return NULL; 50 } 51 bool delete_data_from_hash(_Hash_Table *pHash, int delData){ 52 MyNode *pNode; 53 if((pNode = find_data_in_hash(pHash, delData)) == NULL) return false; 54 MyNode *pHead; 55 if(pNode == (pHead = pHash->value[delData % 10])) pHash->value[delData % 10] = pNode->pNext; 56 else{ 57 while(pHead->pNext != pNode){ 58 pHead = pHead->pNext; 59 } 60 pHead->pNext = pNode->pNext; 61 } 62 delete pNode; 63 return true; 64 } 65 int main(){ 66 67 return 0; 68 }

 

(算法)哈希表

原文:https://www.cnblogs.com/Ljh578519469/p/8748108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!