In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "C++ language how to achieve hash table", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "C++ language how to achieve hash table" bar!
Summary:
The hash table is sometimes called a hash table. Personally, hash table is an intermediate structure between linked list and binary tree. The linked list is very convenient to use, but the data lookup is very troublesome; the data in the binary tree is strictly orderly, but this is the result at the expense of one more pointer. Hash table not only meets the convenience of finding data, but also does not occupy too much content space, and it is also very convenient to use.
For example, all the data is like a lot of books. If these books are stacked one by one, like linked lists or linear tables, the whole data will be very disordered and messy, and you will have to go through a lot of query process before you find the book you need. And if you number all the books and arrange them in order, then if the number of the book you are looking for is n, then after binary search, you will soon find the books you need. But if you don't have many books of each kind, then you can classify these books, which are literature, which are art, which are engineering, and which are science. As long as you simply classify these books, it will be very easy to find a book. For example, if the book you are looking for is a computer book, then you will look for it in the engineering category. It will also be troublesome to find it this way.
I don't know if you are clear about this example, but the classification method mentioned above is actually the essence of the hash table. Next we can write a simple hash operation code.
A) define hash tables and basic data nodes
Typedef struct _ NODE {int data; struct _ NODE* next;} NODE; typedef struct _ HASH_TABLE {NODE* value [10];} HASH_TABLE
B) create the hash table
HASH_TABLE* create_hash_table () {HASH_TABLE* pHashTbl = (HASH_TABLE*) malloc (sizeof (HASH_TABLE)); memset (pHashTbl, 0, sizeof (HASH_TABLE)); return pHashTbl;}
C) look for data in the hash table
NODE* find_data_in_hash (HASH_TABLE* pHashTbl, int data) {NODE* pNode; if (NULL = = pHashTbl) return NULL; if (NULL = (pNode = pHashTbl- > value [data% 10])) return NULL; while (pNode) {if (data = = pNode- > data) return pNode; pNode = pNode- > next;} return NULL;}
D) insert data into the hash table
STATUS insert_data_into_hash (HASH_TABLE* pHashTbl, int data) {NODE* pNode; if (NULL = = pHashTbl) return FALSE; if (NULL = = pHashTbl- > value [data% 10]) {pNode = (NODE*) malloc (sizeof (NODE)); memset (pNode, 0, sizeof (NODE)); pNode- > data = data; pHashTbl- > value [data% 10] = pNode; return TRUE } if (NULL! = find_data_in_hash (pHashTbl, data)) return FALSE; pNode = pHashTbl- > value [data% 10]; while (NULL! = pNode- > next) pNode = pNode- > next; pNode- > next = (NODE*) malloc (sizeof (NODE)); memset (pNode- > next, 0, sizeof (NODE)); pNode- > next- > data = data; return TRUE;}
E) remove data from the hash table
STATUS delete_data_from_hash (HASH_TABLE* pHashTbl, int data) {NODE* pHead; NODE* pNode; if (NULL = = pHashTbl | | NULL = = pHashTbl- > value [data% 10]) return FALSE; if (NULL = = (pNode = find_data_in_hash (pHashTbl, data)) return FALSE; if (pNode = = pHashTbl- > value [data% 10]) {pHashTbl- > value [data% 10] = pNode- > next; goto final } pHead = pHashTbl- > value [data% 10]; while (pNode! = pHead- > next) pHead = pHead- > next; pHead- > next = pNode- > next; final: free (pNode); return TRUE } Thank you for your reading. The above is the content of "how to realize hash table in C++ language". After the study of this article, I believe you have a deeper understanding of how C++ language implements hash table, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.