Refined C algorithm-Hash Table (2)

Source: Internet
Author: User
Previously, the definition, implementation, and analysis of chain hash tables were not as difficult as you thought. just clarify your ideas and draw out his Implementation ideas on the draft paper, the code is easy to read. The next time you look at it, you just need to take out the original sketch and you will know how to implement the previously defined function interfaces: insert, delete, and search. The data structure itself is very abstract. For beginners, it is easy to understand the code implementation idea to draw a sketch. The following describes how to implement another hash table: An on-address hash table. In a chained hash table, elements exist in the bucket of each address. In an open address hash table, elements are stored in the table itself. It is doomed that the conflict cannot be solved like a chain hash table. In the open address hash table, you can resolve the conflict by exploring the table until you find a slot for this element or find this element. Of course, if this element is not found in the whole table, it indicates that the element is not in the table. The efficiency of querying a hash table is mainly related to two factors: the load factor of the hash table (the total number of elements in the bucket) and the degree of uniform distribution of elements. Hash Function Definition for linear profiling: H (K, I) = (H '(k) + I) mod m, k is the key (inserted element ), I indicates the number of probes. I is greater than or equal to 0 and less than M. the number of M slots. H' is an auxiliary hash function. H' = K mod m. the following is a simple example: One of the most effective methods to explore an address hash table is to obtain hash encoding by calculating the sum of the two auxiliary hash functions. The hash function of double hash is defined as H (K, I) = (H1 (k) + ih2 (k) mod m H1 and H2, which are two auxiliary hash functions.

Implementation and analysis of the open address hash table: it is a variable vacated defined in the struct in the Code: personal understanding does not play any substantive role, is to save the location of the last delete operation ~. For more information, see <G id = "1"> </G> ~

 

/* Ohtbl. H */# ifndef ohtbl_h # define ohtbl_h # include <stdlib. h>/* define the structure of the open address hash table */typedef struct ohtbl _ {int positions;/* number of slots */void * vacated; /* the pointer is initialized to point to a special address space to prove that this special address has deleted an element */INT (* H1) (const void * Key ); INT (* H2) (const void * Key); int (* match) (const void * key1, const void * key2); void (* destroy) (void * data ); int size;/* Number of elements */void ** table;/* array of storage elements (malloc application) */} ohtbl; /* function interface */INT ohtbl_init (ohtbl * htbl, int positions, INT (* H1) (const void * Key), INT (* H2) (const void * Key ), INT (* match) (const void * key1, const void * key2), void (* destroy) (void * Data); void ohtbl_destroy (ohtbl * htbl ); int ohtbl_insert (ohtbl * htbl, const void * data); int ohtbl_remove (ohtbl * htbl, void ** data); int ohtbl_lookup (const ohtbl * htbl, void ** data ); # define ohtbl_size (htbl)-> size) # endif
/* Ohtbl. C */# include <stdlib. h> # include <string. h> # include ". /include/ohtbl. H "/* reserve an element whose memory status is freed up */static char vacated;/* struct initialization * return initialization successful: 0, failed:-1. */INT ohtbl_init (ohtbl * htbl, int positions, INT (* H1) (const void * Key), INT (* H2) (const void * Key ), INT (* match) (const void * key1, const void * key2), void (* destroy) (void * Data) {int I; /* The application space is a hash table */If (htbl-> table = (void **) malloc (positions * Sizeof (void *) = NULL) Return-1;/* initialize each positions */htbl-> positions = positions; for (I = 0; I 

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.