C Language implementation of hash table-Open addressing method

Source: Internet
Author: User

Header file:

#ifndef __hashtable_h#define __hashtable_h/********************* (square) Open addressing hashing ***************///if there is a conflict, try another unit, typedef unsigned int index;typedef index position;typedef int elementtype until empty cell is found; #define Mintablesize 5struct hashtable;typedef struct hashTable *hashtable;hashtable initializetable (int tablesize); void destorytable (HashTable H) ;p osition Find (ElementType key,hashtable h); void Insert (ElementType key,hashtable h); ElementType Retrieve (position p,hashtable h); hashTable ReHash (hashTable h); int Delete (ElementType key,hashtable h); int NextPrime (int tablesize); int isprime (int x); int Hash (ElementType key,int tablesize); enum Kindofentry{legitimate,empty , deleted};struct hashentry{elementtype element;enum kindofentry info;}; typedef struct HASHENTRY cell;struct hashtable{int Tablesize;cell *thecells;};
#endif


Implementation file:

#include "HashTable.h" #include <stdio.h> #include <stdlib.h>/*********************** development Method (square) ********* /hashtable initializetable (int tablesize) {hashTable table;int i;if (tablesize< Mintablesize) {printf ("Table size is too small\n"); exit (-1);} Table= (hashTable) malloc (sizeof (struct hashTable)), if (!table) {printf ("Out of space\n"), Exit (-1);} Table->tablesize=nextprime (tablesize); table->thecells= (cell*) malloc (sizeof (cell) *table->tablesize); (!table->thecells) {printf ("Out of space\n"); exit (-1);} for (i=0;i<table->tablesize;i++) Table->thecells[i].info=empty;return table;} void Destorytable (hashTable H) {free (h->thecells); H=null;} Position find (ElementType key,hashtable H) {position currentpos;int Collisionnum;collisionnum=0;currentpos=hash (key, H->tablesize); while (H->thecells[currentpos].info!=empty&&h->thecells[currentpos].element!=key ) {//Squared detection method Currentpos+=2*collisionnum-1;if (currentpos>=h->tablesize) currentpos-=h-> tablesize;} return currentpos;} int Delete (ElementType key,hashtable H) {position pos;pos=find (key,h); if (H->thecells[pos].info==empty) return 0; Else{h->thecells[pos].info=deleted;return 1;}} void Insert (ElementType key,hashtable H) {position pos;pos=find (key,h); if (h->thecells[pos].info!=legitimate) {h- >theCells[pos].info=legitimate; H->thecells[pos].element=key;}} ElementType Retrieve (position p,hashtable h) {}hashtable ReHash (hashTable h)//re-hash {int I,oldsize;cell *oldcells;o ldcells=h->thecells;oldsize=h->tablesize; H=initializetable (2*oldsize); for (i=0;i<oldsize;i++) {if (oldcells[i].info==legitimate) insert (Oldcells[i]. ELEMENT,H);} Free (oldcells); return H;} int nextprime (int tablesize) {while (1) {if (IsPrime (tablesize)) return tablesize;elsetablesize++;}} int isprime (int x) {int i;for (i=2;i*i<=x;i++) {if (x%i==0) return 0;} return 1;} int Hash (ElementType key,int tablesize) {return key%tablesize;} /**************************************************************************/



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

C Language implementation of hash table-Open addressing method

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.