C Language Implementation HashTable

Source: Internet
Author: User

Simple implementation of the hash table insert and lookup function, briefly described as follows:

1. Data structure:

struct Hashnode

{

char* SKey; Key

int nvalue; Value

hashnode* Pnext; When the hash value conflicts, point to the next node with the same hash value.

}

hashnode* Hashtable[hash_table_max_size]; An array of hash tables

int hash_table_size; Number of elements in a hash table

2, Function:

void Hash_table_init () initialization hash table

The void Hash_table_insert (const char* skey, int nvalue) inserts a key skey into the hash table, and the value is Nvalue.

The key value pair is ignored when Skey is already in the hash table.

void Hash_table_remove (const char* skey) Deletes a key-value pair from the hash table.

hashnode* hash_table_lookup (const char* skey) finds a node with a key value of Skey. When found, returns the corresponding Hashnode pointer,

Returns NULL when not found.

void Hash_table_release () frees the memory space of the hash table.

C language implementation of the hash table (HashTable) source is as follows:

/*
* Author:puresky
* date:2011/01/08
* Purpose:a simple implementation of HashTable in C
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*=================hash Table start=========================================*/

#define HASH_TABLE_MAX_SIZE 10000
typedef struct HASHNODE_STRUCT Hashnode;

struct HASHNODE_STRUCT
{
char* SKey;
int nvalue;
hashnode* Pnext;
};

hashnode* Hashtable[hash_table_max_size]; Hash table Data Strcutrue
int hash_table_size; The number of key-value pairs in the hash table!

Initialize Hash table
void Hash_table_init ()
{
hash_table_size = 0;
memset (hashTable, 0, sizeof (hashnode*) * hash_table_max_size);
}


String hash function
unsigned int hash_table_hash_str (const char* skey)
{
Const signed CHAR *p = (const signed char*) Skey;
unsigned int h = *p;
if (h)
{
for (p + 1; *p!= '; ++p ')
h = (H << 5)-H + *p;
}
return h;
}

Insert Key-value into hash table
void Hash_table_insert (const char* skey, int nvalue)
{
if (hash_table_size >= hash_table_max_size)
{
printf ("Out of hash Table memory!\n");
Return
}

unsigned int pos = HASH_TABLE_HASH_STR (skey)% Hash_table_max_size;

hashnode* phead = Hashtable[pos];
while (Phead)
{
if (strcmp (Phead->skey, sKey) = = 0)
{
printf ("%s already exists!\n", skey);
return;
}
Phead = phead->pnext;
}

hashnode* Pnewnode = (hashnode*) malloc (sizeof (Hashnode));
memset (pnewnode, 0, sizeof (hashnode));
Pnewnode->skey = (char*) malloc (sizeof (char) * (strlen (SKey) + 1));
strcpy (Pnewnode->skey, SKey);
Pnewnode->nvalue = Nvalue;

Pnewnode->pnext = Hashtable[pos];
Hashtable[pos] = Pnewnode;


hash_table_size++;
}
Remove Key-value frome the hash table
void Hash_table_remove (const char* skey)
{
unsigned int pos = HASH_TABLE_HASH_STR (skey)% Hash_table_max_size;
if (Hashtable[pos])
{
hashnode* phead = Hashtable[pos];
hashnode* pLast = NULL;
hashnode* premove = NULL;
while (Phead)
{
if (strcmp (skey, phead->skey) = = 0)
{
Premove = Phead;
Break
}
PLast = Phead;
Phead = phead->pnext;
}
if (Premove)
{
if (pLast)
Plast->pnext = premove->pnext;
Else
Hashtable[pos] = NULL;

Free (Premove->skey);
Free (premove);
}
}
}

Lookup a key in the hash table
hashnode* hash_table_lookup (const char* skey)
{
unsigned int pos = HASH_TABLE_HASH_STR (skey)% Hash_table_max_size;
if (Hashtable[pos])
{
hashnode* phead = Hashtable[pos];
while (Phead)
{
if (strcmp (skey, phead->skey) = = 0)
return phead;
Phead = phead->pnext;
}
}
return NULL;
}

Print the content in the hash table
void Hash_table_print ()
{
printf ("===========content of Hash table=================\n");
int i;
for (i = 0; i < hash_table_max_size; ++i)
if (Hashtable[i])
{
hashnode* phead = Hashtable[i];
printf ("%d=>", I);
while (Phead)
{
printf ("%s:%d", Phead->skey, Phead->nvalue);
Phead = phead->pnext;
}
printf ("\ n");
}
}

Free the memory of the hash table
void Hash_table_release ()
{
int i;
for (i = 0; i < hash_table_max_size; ++i)
{
if (Hashtable[i])
{
hashnode* phead = Hashtable[i];
while (Phead)
{
hashnode* ptemp = Phead;
Phead = phead->pnext;
if (ptemp)
{
Free (Ptemp->skey);
Free (ptemp);
}

}
}
}
}

/* ===============================hash Table end=========================*/


/* ============================test function ============================*/
#define Max_str_len 20
#define Min_str_len 10
void Rand_str (char r[])
{
int i;
int len = Min_str_len + rand ()% (Max_str_len-min_str_len);
for (i = 0; i < len-1; ++i)
R[i] = ' A ' + rand ()% (' Z '-' a ');
R[len-1] = ' the ';
}

int main (int argc, char** argv)
{
Srand (Time (NULL));
Hash_table_init ();
printf ("Insert testing.........\n");
int n = 10;
const char *key1 = "AAAMMD";
const char *key2 = "Xzzyym";
const char *key3 = "cdcded";

Hash_table_insert (Key1, 110);
Hash_table_insert (Key2, 220);
Hash_table_insert (Key3, 330);
Char Str[max_str_len + 1];
while (n--)
{
Rand_str (str);
Hash_table_insert (str, n);
}
Hash_table_print ();

printf ("\nlookup testing..........\n");
hashnode* Pnode = Hash_table_lookup (Key1);
printf ("Lookup result:%d\n", pnode->nvalue);
Pnode = Hash_table_lookup (Key2);
printf ("Lookup result:%d\n", pnode->nvalue);

printf ("\nremove testing..........\n");
printf ("Before Remove%s:\n", Key3);
Hash_table_print ();
Hash_table_remove (Key3);
printf ("After remove:\n");
Hash_table_print ();
Hash_table_release ();

System ("pause");
return 0;
}

The results of the operation are as follows:

From: http://hi.baidu.com/gropefor/blog/item/c306aceee09738fdce1b3e18.html

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.