<pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" > <strong > Hash list </strong> (hash table, also known as a hash table) is a data structure that is accessed directly from the key value. That is, it accesses records by mapping key code values to a location in the table to speed up lookups. This mapping function is called a hash function, and the array that holds the record is called the hash table. given table M, there is a function f (key), for any given keyword value key, after substituting the function if you can get the address of the record containing the keyword in the table, then the table M is the <strong> hash (hash) table </strong>, function f ( Key) is the <strong> hash function </strong>. A hash table is a one-dimensional array, and an array element is a struct: a struct has a node pointer element, can have a data field, can have multiple data fields, or it can have no simple hash table: hash_table = sequential structure + chain structure <strong> Conflict:</strong> elements x, y, Z are equal after P (the default hash table length), i.e. should be stored under the same subscript <strong> workaround:</strong> Insert y into a node, The node hangs below the array element labeled X%p, and z hangs below Y.
The following is the implementation of the hash table, inserting and printing code: #include <iostream>using namespace std; #define P 7//hash table default Tables long # # 1//Silent The initialized value of # define ELEMTYPE int//value type typedef struct BUCKET_NODE//node type {elemtype data[3];struct bucket_node *next; Each node can hold 3 data and one pointer pointing to another node}bucket_node;typedef Bucket_node hash_table[p]; Define a hash table (actually an array)/* Initialize the hash table (array) element */void Init_bucket_node (hash_table &ht, int i) {memset (&ht[i], $, sizeof ( Elemtype); ht[i].next = 0;} /* Initialize hash table */void init_hash_table (hash_table &ht) {for (int i = 0; i<p; ++i) Init_bucket_node (HT, i);} /* hash function, which is the subscript */int hash (Elemtype x) {return x% P;} that the element x should be inserted into the hash table. /* insert element x into hash table HT */int insert_new_element (hash_table &ht, elemtype x) {int index = hash (x); Gets which subscript of element x should be inserted into the hash table for (bucket_node* p = &ht[index],*q = NULL; Null!=p; Q=p,p=p->next) {//node pointer p points to ht[index] node if (null! = P->next)//if P->next = = NULL, stating that three data for the node referred to by P is already full C Ontinue; Then P refers to the next node for (int i = 0; i<3; ++i) {if ($ = = P->data[i])//If a location equals $, indicating that the location is empty, insert X and return {p->data[i] = X;return 0;}} if (null = = p)//if p = = NULL, indicating that the last node is exactly full and requires another node of memory {Bucket_node * s = new Bucket_node;memset (s, $, sizeof (Elemtype); Initialize the newly opened memory S->next = null;s->data[0] = x; X put in the first position of the node Q->next = s; Hang S to the bottom of the last node, return 0; return-1; If the location is not found under the existing node and the node fails, X will not really insert the}/* print hash table */void show_hash_table (hash_table &ht) {bucket_node* p = null;for (int i = 0; i<p; ++i)//start printing from the first hash table element, know the last element {cout<< "ht[" <<i<< "]" << ":"; for (p=&ht[i]; Null!=p; P=p->next)//print the hash table element labeled I and the node that the location is linked to {for (int k = 0; 3>k && $!=p->data[k]; ++k)//p->data[k]! = $, Indicates that the location is not empty, then print {cout<<p->data[k]<< "";}} Cout<<endl;}}
Here is the test code: Main.cppvoid Main () {hash_table ht; Elemtype ar[] = {0,7,14,21,28,59,35,42,5,9,3,6,13,52,64}; Test array init_hash_table (HT); Initialize the hash table for (int i = 0; i<sizeof (AR)/sizeof (*ar); ++i) Insert_new_element (HT, ar[i]); Array elements are inserted sequentially into the hash table show_hash_table (HT); Print hash table cout<< "over" <<ENDL;}
<pre name= "code" class= "CPP" > If there is more data (enough), the inserted element should be evenly distributed in the hash table, i.e. the chain length after each node element should be equal.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hash table and hash function C implementation