Hash table
- Hashing, also known as hashing, is a special search method
(hash method wants to get elements in one access without any comparison)
(hash is to store data by category, and then quickly find a certain data from a large number of data)
Design steps of hash table
- Determine the spatial range of the table and the hash value field
- Construct an appropriate hash function (the function needs to ensure that all elements in the table are calculated by the function, and the return value of the function is within the value range
- Choose a way to handle conflicts (conflicts mean that a data has the same multiple existence)
Coding design: self function (even if the variable is also a value) or digital analysis method (method of finding redundancy and superposition)
(it can be understood that the process of numbering and checking a book in order to find another book in the Library)
2. Implementation code of hash table
#include<stdio.h> #include<memory.h> struct MyNode//Chain structure (used to handle conflicts { int data; MyNode *pNext; }; struct HashTable { MyNode *val[10];//Define an array of pointers, each of which is understood as the head pointer of the linked list. }; HashTable* createHashTable()//Create a hash table { HashTable *pHash = new HashTable; memset(pHash, 0, sizeof(HashTable));//Assign 40 bytes of heap area to 0 value of each byte return pHash; } //Hash table insert function bool insertHashTable(HashTable *pHash,int data) { if (pHash == nullptr)return false; MyNode* pNode; pNode = pHash->val[data % 10]; if (pNode == nullptr) { pHash->val[data % 10] = new MyNode; pHash->val[data % 10]->data = data; pHash->val[data % 10]->pNext = nullptr; return true; } else { while (pNode->pNext)pNode = pNode->pNext; pNode->pNext = new MyNode; pNode->pNext->data = data; pNode->pNext->pNext = nullptr; return true; } return true; } //Search element MyNode * findDataInHash(HashTable* pHash, int findData) { if (pHash == nullptr)return nullptr; MyNode* pNode = pHash->val[findData % 10]; if (pNode == nullptr)return nullptr; while (pNode) { if (pNode->data == findData)return pNode; pNode = pNode->pNext; } return nullptr; } //Delete elements bool deleteDataFromHash(HashTable* pHash, int data) { MyNode* pNode = findDataInHash(pHash, data); if (pNode == nullptr)return false; MyNode* pHead = pHash->val[data % 10]; if (pHead == pNode) { pHash->val[data % 10] = pNode->pNext; } else { while (pHead->pNext!=pNode) { pHead = pHead->pNext; } pHead->pNext = pNode->pNext; } delete pNode; return true; } //Empty elements void clearHashTable(HashTable*& pHash) { if (pHash == nullptr)return; MyNode* pHead; MyNode* pNode; for (int i = 0; i < 10; ++i) { if ((pHead = pHash->val[i]) != nullptr) { while (pHead) { pNode = pHead; pHead = pHead->pNext; delete pNode; } } } delete[] pHash; pHash = nullptr; } int main() { HashTable *pHash = nullptr; pHash = createHashTable(); insertHashTable(pHash, 1); insertHashTable(pHash, 10); insertHashTable(pHash, 11); insertHashTable(pHash, 32); insertHashTable(pHash, 21); deleteDataFromHash(pHash, 11); deleteDataFromHash(pHash, 1); getchar(); return 0; }