In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to realize the linear detection method of C++ hash table. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
1. Hash table-linear detection theory
The theory of linear detection has been explained in the last blog post.
Now let's take a look at the code idea of linear detection:
1.1. Additional elements of the hash table
Note:
When traversing back to find a free location, pay attention to circular traversing! Otherwise, the access array will be out of bounds.
When you add an element and the location is occupied, that is, after a hash conflict, when you traverse backwards to find a free location, we need to know that there are two situations in which this free location is available:
1. This position is always empty and does not let go of the elements.
2. This position is empty. The element was let go before, but was deleted later.
1.2. Query operation of hash table
When the subscript value calculated by the hash function is 3, and then access the array, when querying, it is found that the value is not equal to the value val of the element to be queried, indicating that there was a hash conflict when the val was put, so it is time to traverse backwards.
When you visit the 4 subscript, you find that the location is empty (there are two empty cases). If the location is empty all the time, you don't have to look backwards, val doesn't exist! Because it is a linear detection method, so if you want to put the val at that time, it must be put here.
But if this location is empty, but the element was let go before, and then deleted, the element was stored in this location, and then when val was inserted, it was inserted into the free location behind it, so at this point we have to continue to iterate back to look for the Val value.
So we need to define a Bucket node to represent all the contents of each element.
/ / the status of the bucket enum State {STATE_UNUSE, / / the bucket STATE_USING that has never been used, / / the bucket in use is a valid element, and the STATE_DEL has not been deleted, / / the bucket whose element has been deleted is considered invalid} / / when we delete the elements in the bucket, we don't really delete the value, but if we set the state of the bucket to STATE_DEL, we think that the elements in the bucket are invalid / / the type of bucket struct Bucket {Bucket (int key = 0, State state = STATE_UNUSE): key_ (key), state_ (state) {} int key_ / / current status of stored data State state_; / / bucket}; 1.3.deletion of hash table
2. Hash table-linear detection code implementation 2.1, prime number in prime table
Code for finding primes: (used for primes in prime tables)
Int main () {int data = 3; for (int I = data; I < 10000; iSj +) {int j = 2; for (; j < I; jaunt +) {if (I% j = = 0) break } if (j = = I) cout
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.