[C + +] data structures for separating and linking the hash table

Source: Internet
Author: User

  Hash list, called hash table in English, is also called a hash table, is a data structure based on the key value to determine the storage location in main memory. A hash function (a function of the key value) is adopted to determine where to store the keyword.

The main methods are: 1. Separating link method (Zipper method)

The hash function for the detach link method is position = key% n. That is, the keyword's storage location is the value of the keyword to model the number of table items. If the table item size is 13, for items with a key value of 27, the table key is stored at 1 (27 13 = 1). To reduce conflicts, n tends to take primes. , using a queue (linked list) to join together, the newly placed elements into the team head or the end of the team can be. The picture is described as follows:

  2. Linear probing method

Linear probing is based on the number of collisions, to avoid collisions from the target position after the collision, that is, position_new = (position + i)% n, where I is the number of collisions, n is the table item size.

3. Square Probing method

Similar to linear probing, except that the hash function is different, its position_new = (position + i2)% n

4. Double hash, re-hash, etc.

 The structure of the data structure of the data structure that separates the link method uses a class named Hashtable to encapsulate the operations and properties of the entire table. Use vector<deque<int>> to represent the entire data structure, that is, the entire table is a single (double-ended)

A vector (array) of queues. The class also contains other functions that are used to access private properties in the class.

The declaration of the class is as follows:

1 classHashTable {2      Public:3HashTable (int= One);4~hashtable () =default;5         intGethashitemsize (Const int)Const;//Get Queue Length6         intGethashtablesize ()Const;//get the size of a table item7         BOOLInsertrecord (int);//Insert a value8         voidRemoveRecord (Const int);//Delete a value9         BOOLIsEmpty ()Const;//determine if the hash table is emptyTen         voidPrint ()Const;//Print Hash Table One  A     Private: -vector<deque<int>> Hashitem;//structure Definition -         intHashtablesize =0;//Number of hash table entries the};

  

Constructor definition:

  

1Hashtable::hashtable (intN//constructor Definition2 {3Hashtablesize =N;4 5      for(inti =0; I < n; ++i)6     {7deque<int>Adeque;8Hashitem.push_back (Adeque);//to push a sufficient empty queue into the vector9     }Ten}

The definition of the function that inserted the record:

1 BOOLHashtable::insertrecord (intData//inserts a record of a specified value2 {3     intPosition = data% Hashtablesize;//Mapping Rules4 5     if(Position >= Hashtablesize | | position <0)//Judgment of legality6         return false;7 8     Else9     {Tenhashitem.at (position). Push_front (data);//according to the principle of time locality, inserting into the team head One         return true; A     } -}

Delete a function definition for a record

1 voidHashtable::removerecord (Const intAim//Delete a record of a specified value2 {3     inti =0;4     intj =0;5 6      for(; i < hashtablesize; ++i)//traversing table Entries7     {8 9          for(j =0; J < static_cast<int> (hashitem.at (i). Size ()); ++J)//Traverse QueueTen         { One             if(Hashitem.at (i). at (j) = =aim) Ahashitem.at (i). Erase (hashitem.at (i). Begin () + j);//Deleting Records -         } -     } the  -}

Print function:

1 voidHashTable::p rint ()Const2 {3      for(inti =0; I < gethashtablesize (); ++i)//traversing table Entries4     {5deque<int> temp =hashitem.at (i);6 7          for(Auto &j:temp)//Traverse Queue8cout << J <<' ';9 Tencout <<Endl; One     } A}

For the main function, write a little test statement to test:

1 intMain ()2 {3HashTable HashTable ( -);4 5      for(inti =0; I < -; ++i)6 Hashtable.insertrecord (i);7 8 hashtable.print ();9 Tencout <<Endl; One  AHashtable.removerecord ( the); -Hashtable.removerecord ( -); -  the hashtable.print (); -  -     return 0; -}

Insert 0 to 99 keywords, hash to the table item size 13 in the hash list, delete the 91 and 702 key values.

The run results output a hash of 0 to 99 and a hash after the delete operation:

I original , reproduced please indicate the source, thank you for your cooperation!

[C + +] data structures for separating and linking the hash table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.