算法:开放定址法散列表

hash.h

#ifndef _HASHQUAD_H
#define _HASHQUAD_H

#define MinTableSize 10

struct HashEntry;
struct HashNode;

typedef char *ElementType;
typedef unsigned int Index;
typedef Index Position;
typedef struct HashNode *HashTable;
typedef struct HashEntry *Cell;

enum KindOfEntry{Legitimate,Empty,Deleted};

HashTable InitializeHashTable(int TableSize);
void DestoryTable(HashTable H);
Position Find(ElementType key,HashTable H);
void Insert(ElementType key,HashTable H);
void Delete(ElementType key,HashTable H);
Index Hash(const char *key,int TableSize);
HashTable Rehash(HashTable H);

#endif

struct HashEntry
{
    ElementType key;
    KindOfEntry info;//此处为懒惰删除域 
};

struct HashNode
{
    int TableSize;
    Cell *TheCells;//执行Cell的指针数组 
};

hash.c

#include <stdlib.h>
#include <stdio.h>
#include "Hash1.h"

//生成下个素数 
static unsigned int NextPrime(int N)
{
    if(N%2==0)
        N++;
    for(;;N+=2){
        for(i=3;i*i<=N;i+=2){
            if(N%i==0){
                goto ContOuter;
            }
        }
        return N;
        ContOuter:;
    }
}
Index Hash(const char *key,int TableSize)
{
    unsigned int HashVal=0;
    while(*key!='\0'){
        HashVal=(HashVal<<5)+*key++;
    }
    return HashVal%TableSize;
}
HashTable InitializeHashTable(int TableSize)
{
    HashTable H;
    H=(HashTable)malloc(sizeof(struct HashNode));
    if(H==NULL){
        printf("out of space");
        return NULL;
    }
    H->TableSize=NextPrime(TableSize);
    //分配地址空间
    H->TheCells=malloc(sizeof(Cell))*H->TableSize;
    if(H->TheCells==NULL){
        printf("out of space");
        return NULL;
    }
    for(i=0;i<H->TableSize;++i){
        H->TheCells[i]->info=Empty;
    }
    return H;
}
Position Find(ElementType key,HashTable H)
{
    Position CurrentPos;
    int CollisionNum=0;
    CurrentPos=Hash(key,H);
    while(H->TheCells[CurrentPos]->info!=Empty&&strcmp(H->TheCells[CurrentPos].info,key)!=0){
        //平方探测函数由  F(i) = F(i-1) + 2 * i - 1  
        CurrentPos+=2*++Collision-1;
        if(CurrentPos>TableSize){
            CurrentPos-=TableSize;
        }
    }
    return CurrentPos;
}
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]->key=malloc(strlen(key)+1);
        strcpy(H->TheCells[Pos]->key,key);
        H->TheCells[Pos]->key[strlen(key)]='\0';
    }
}
void Delete(ElementType key,HashTable H)
{
    Position Pos;
    Pos=Find(key,H);
    if(H->TheCells[Pos].info==Legitimate){
        H->TheCells[Pos].info=Legitimate;        
    }
}
void DestoryTable(HashTable H)
{
    free(H->TheCells);
    free(H);
}
HashTable Rehash(HashTable H)
{
    int i,OldSize;
    Cell *OldCells;
    OldCells=H->TheCells;
    OldSize=H->TableSize;
    H=IntializeHashTable(2*OldSize);
    //把原来表数据拷贝过来
    for(i=0;i<OldSize;++i){
        if(OldCells[i]->info==Legitimate){
            Insert(OldCells[i]->info,H);
        }
        free(OldCells);
    }
    return H;
}

 

转载于:https://www.cnblogs.com/peixiguang/p/6661466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值