Hash open addressable Method C implementation

Source: Internet
Author: User

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");


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.