C-language implementation of a hash table-Separate link method

Source: Internet
Author: User

One: the definition of a hash table:

The implementation of a hash table is often called hashing, which is a technique used to perform insertions, lookups, and deletions with constant average time, but those operations that require any sort information between elements will not be supported, such as findmin,Findmax and so on. the advantages of the hash table is obvious, its query time is constant, the speed is very fast, the disadvantage is that there is no ordering between elements, for some need to sort the occasion is not applicable. An ideal hash table data structure is a fixed-size array that contains keywords, with a hash function that is used to insert keywords into the table using the values of the keywords. This is the basic idea of hashing, the remaining problem is to choose a good hash function, when two keywords hash to the same value should be how to deal with and how to determine the size of the hash table.

Two: Select the hash function:

1. If the input keyword is an integer, the general method is to return directly to key mod tablesize;(hashval=key%tablesize;), the best choice for tablesize is prime, because there is no other approximate number of elements. If the size of the table is a number like this, if the keyword is 0 for everyone, then all the keywords will be mapped to the same location.


2. The generic hash keyword is a string.

A hash function is the addition of the ASCII code for all characters of a string . However, because the maximum ASCII value is only 127, the range that can be mapped is not large enough.


There is another way:


It assumes that the key has at least two characters plus a null character, which only considers the first three characters, and if they are random, the function can distribute the keyword evenly within a small range, but the real use of the Chinese and English letters is not random. So this function is not appropriate when the hash table is large enough.

There is one such thing:


This function can generally be assigned very well.

Detach Link Method:

Separate join method a simple way to implement a hash table. The practice is to hash all values in the same position

Are saved to a linked list. The implementation method is also relatively simple, many operations are linked list operations.

Header file:

#ifndef __hashtable_h#define __hashtable_h/**************** Separation Chain Table method ************************/struct ListNode;typedef struct ListNode *position;typedef position list;struct hashtable;typedef struct HashTable *hashtable;typedef int ElementType; #define Mintablesize 5#define maxtablesize 20hashTable initializetable (int tablesize); void Destroytable ( HashTable h);p osition find (ElementType key,hashtable h); ElementType Retrieve (position p); int nextprime (int tablesize); int isprime (int x); void Insert (ElementType key,hashtable h); int Hash (ElementType key,int tablesize); int Delete (ElementType key,hashtable H); struct Listnode{elementtype element ;p osition next;}; struct Hashtable{int tablesize;list *thelists;//pointer to listnode pointer};/********************************************** /#endif

Implementation file:

#include "HashTable.h" #include <stdio.h> #include <stdlib.h>/*************************** separation Chain List method ********* * Constructs a pointer array, each element of the array points to a linked list */hashtable initializetable (int tablesize) {hashTable h;int i;if ( Tablesize<mintablesize) {printf ("Table is too small!\n"); return NULL;} h= (struct hashtable*) malloc (sizeof (struct HashTable)), if (h==null) {printf ("Out of space\n"); return NULL;} H->tablesize=nextprime (tablesize);//11//Create pointer array h->thelists=malloc (sizeof (list) *h->tablesize);//pointer array, The data is not placed in the array, the data is placed in the list if (h->thelists==null) {printf ("Out of space!\n"); return NULL;} Creates a pointer to an array of elements that point to the list header for (i=0;i
Test:

#include "HashTable.h" #include <stdio.h>int main () {/******************** hash separated link method test ************************** **//*hashtable table;position p;table=initializetable (n); insert (0,table); insert (1,table); insert (81,table); Insert (4,table), insert (64,table), insert (25,table), insert (16,table), insert (36,table), insert (9,table), insert (49, Table);p =find (81,table), if (p==null) printf ("Cant find 1\n"), elseprintf ("Find%d \ n", p->element); if (Delete (81, Table) {printf ("Delete 81\n");} P=find (81,table), if (p==null) printf ("Cant find 81\n"), elseprintf ("Find%d \ n", p->element);d estroytable (table);


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C-language implementation of a hash table-Separate link method

Related Article

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.