Header file
#ifndef _open_hash_h
#define _OPEN_HASH_H
#define Minnum
struct Node;
struct HASHTBL;
Enum kind {legeal,empty,deleted};
typedef struct NODE Cell;
typedef struct HASHTBL *hashtable;
typedef int ITEM;
static int hash (Item item,int tablesize);
HashTable initial (int size);
void Destroy (HashTable H);
int find (Item item,hashtable H);
void Insert (Item item,hashtable H);
void DeleteItem (Item item,hashtable H);
#endif
struct node{
item item;
Enum kind info;
struct hashtbl{
int tablesize;
Cell *thecells;
};
source files
#include "open_hash.h" #include <stdio.h> #include <stdlib.h> static int hash (Item item,int tablesize) {Retu
RN Item%tablesize;
} HashTable initial (int size) {if (size < Minnum) {printf ("size too small");
Exit (1);
} HashTable H;
H=malloc (sizeof (struct hashtbl));
if (h==null) {printf ("Arrange hashtable fail");
Exit (1);
} h->tablesize=size;
H->thecells=malloc (sizeof (struct Node) *h->tablesize);
if (h->thecells==null) {printf ("Arrange array fail");
Free (H);
Exit (1);
} int i;
for (i=0;i!=h->tablesize;i++) {h->thecells[i].info=empty;
return H;
} void Destroy (HashTable H) {free (h->thecells);
Free (H);
int find (Item item,hashtable H) {int Pos=hash (item,h->tablesize);
int collision=0;
while (H->thecells[pos].info!=empty && h->thecells[pos].item!=item) {pos+=2*++collision-1;
if (pos>=h->tablesize) pos-=h->tablesize;
return POS; } void Insert (Item item,hashtable H) {int Pos=find (itEM,H);
if (h->thecells[pos].info!=legeal) {h->thecells[pos].info=legeal;
h->thecells[pos].item=item;
} void DeleteItem (Item item,hashtable H) {int pos = find (ITEM,H);
if (h->thecells[pos].info==legeal) h->thecells[pos].info=deleted; }
Test routines
#include "open_hash.h"
#include <stdio.h>
#include <stdlib.h>
int main () {
HashTable h= Initial (a);
int pos;
Pos=find (6,h);
if (h->thecells[pos].info ==empty)
printf ("No such a key\n");
Insert (6,h);
Insert (63,h);
Insert (61,h);
Insert (26,h);
Pos=find (6,h);
if (h->thecells[pos].info ==legeal)
printf ("has such a key\n");
DeleteItem (6,h);
Pos=find (6,h);
if (h->thecells[pos].info ==deleted)
printf ("has delete such a key\n");