One: the definition of a hash table:
The implementation of a hash table is often called a hash, a technique used to perform inserts, lookups, and deletions with a constant average of time, but operations that require any sort of information between elements will not be supported, such as Findmin,findmax and so on. The advantage of the hash table is obvious, its query time is constant, very fast, the disadvantage is that there is no sorting between elements, for some need to sort the occasion does not apply. The ideal hash data structure is a fixed-size array containing keywords, and a hash function to follow the keyword values to insert the keyword into the table. This is the basic idea of hashing, and the rest of the problem is to choose a good hash function, when the two keywords hash to the same value should be how to deal with and how to determine the size of the hash table.
Second: Select the hash function:
1. If the input keyword is an integer, the general method is to return the key mod tablesize directly (hashval=key%tablesize;), the tablesize choice is prime, because there are no other divisors. If the size of the table is 10, if the keyword is 0 for everyone, all the keywords will be mapped to the same location.
2. The generic hash keyword is a string.
A hash function adds the ASCII code of all characters in a string. However, because the maximum ASCII value is only 127, the range that can be mapped is not large enough.
Another one is this:
It assumes that the key has at least two characters plus a null character, which takes into account only the first three characters, and if they are random, the function can evenly distribute the keyword in a small range, but the real use of the English alphabet is not random. So this function is not appropriate when the hash table is large enough.
Another one is this:
This function can generally be well distributed.
Separate link method:
A simple hash-table implementation method for separating the connection. The practice is to hash all values in the same location
Are saved to a linked list. The implementation method is also relatively simple, many operations are linked list operation.
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
hashTable initializetable (int tablesize);
void Destroytable (hashTable H);
Position 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;
Position next;
struct HashTable
{
int tablesize;
List *thelists; Pointer to listnode pointer
};
/**************************************************/
#endif
Implementation file:
#include "HashTable.h" #include <stdio.h> #include <stdlib.h>/*************************** separation linked list method *********
///////* Constructs an array of pointers, each of which points to a 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 array does not put the data, the data is placed in the list if (h->thelists==null) {printf ("Out of space!\n");
return NULL; ///create pointer array element point to the linked table header for (i=0;i
Test:
#include "HashTable.h"
#include <stdio.h>
int main ()
{
/******************** hash separation link Method test //
* hashTable table;
Position p;
Table=initializetable (ten);
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");
else
printf ("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");
else
printf ("Find%d \ n", p->element);
Destroytable (table);