(算法)哈希表


  主函数没有代码!!!

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 }

 

posted @ 2018-04-08 22:54  ♚奋斗的小丑  阅读(191)  评论(0编辑  收藏  举报