分离链式法
#include<stdio.h>
#include<stdlib.h>
#define MinTableSize (10)
#ifndef _HashSep_H
typedef int ElementType;
typedef unsigned int Index;
struct ListNode;
typedef struct ListNode* Position;
struct HashTbl;
typedef struct HashTbl* HashTable;
Index Hash(int Key, int TableSize);
HashTable InitializeTable(int TableSize);
void DestroyTable(HashTable H);
Position Find(ElementType Key, HashTable H);
void Insert(ElementType Key, HashTable H);
ElementType Retrieve(Position P);
#endif
struct ListNode
{
ElementType Element;
Position Next;
};
typedef Position List;
struct HashTbl
{
int TableSize;
List *TheLists;
};
Index Hash(int Key, int TableSize)
{
return Key % TableSize;
}
static int NextPrime(int N)
{
int i;
if (N % 2 == 0)
N++;
for ( ; ; N += 2)
{
for (i = 3; i * i <= N; i+= 2)
{
if (N % i == 0)
break;
}
return N;
}
}
HashTable InitializeTable(int TableSize)
{
HashTable H;
int i;
if (TableSize < MinTableSize)
{
printf("Table size too small");
return NULL;
}
H = (struct HashTbl*)malloc(sizeof(struct HashTbl));
if (NULL == H)
{
printf("H is NULL");
return NULL;
}
H->TableSize = NextPrime(TableSize);
H->TheLists =(List*) malloc(sizeof(List) * H->TableSize);
if (NULL == H)
{
printf("H is NULL");
return NULL;
}
for (i = 0; i < H->TableSize; i++)
{
H->TheLists[i] = (struct ListNode*)malloc(sizeof(struct ListNode));
if (NULL == H->TheLists[i])
{
printf("H is NULL");
return NULL;
}
else
{
H->TheLists[i]->Next = NULL;
}
}
return H;
}
Position Find(ElementType Key, HashTable H)
{
Position P;
List L;
L = H->TheLists[Hash(Key, H->TableSize)];
P = L->Next;
while(P != NULL && P->Element != Key)
{
P = P->Next;
}
return P;
}
void Insert(ElementType Key, HashTable H)
{
Position Pos, NewCell;
List L;
Pos = Find(Key, H);
if (NULL == Pos)
{
NewCell = (struct ListNode*)malloc(sizeof(struct ListNode));
if (NULL == NewCell)
{
printf("Distribute failed!");
return;
}
else
{
L = H->TheLists[Hash(Key, H->TableSize)];
NewCell->Next = L->Next;
NewCell->Element = Key;
L->Next = NewCell;
}
}
}
void DestroyTable(HashTable H)
{
int i;
for (i = 0; i < H->TableSize; i++)
{
Position P = H->TheLists[i];
Position Tmp;
while(P != NULL)
{
Tmp = P->Next;
free(P);
P = Tmp;
}
}
free(H->TheLists);
free(H);
}
ElementType Retrieve(Position P)
{
return P->Element;
}
int main()
{
system("pause");
return 0;
}
开放寻址法
#include <stdlib.h>
#include <stdio.h>
#define MinTableSize 10
#ifndef _HashSep_H
typedef int ElementType;
typedef unsigned int Index;
typedef Index Position;
struct HashTbl;
typedef struct HashTbl* HashTable;
Index Hash(int Key, int TableSize);
HashTable InitializeTable(int TableSize);
void DestroyTable(HashTable H);
Position Find(ElementType Key, HashTable H);
void Insert(ElementType Key, HashTable H);
ElementType Retrieve(Position P);
HashTable Rehash(HashTable H);
#endif
enum KindOfEntry {Legitimate, Empty, Deleted};
struct HashEntry
{
ElementType Element;
enum KindOfEntry Info;
};
typedef struct HashEntry Cell;
struct HashTbl
{
int TableSize;
Cell* TheCells;
};
static int NextPrime(int N)
{
int i;
if (N % 2 == 0)
N++;
for ( ; ; N += 2)
{
for (i = 3; i * i <= N; i+= 2)
{
if (N % i == 0)
break;
}
return N;
}
}
Index Hash(ElementType Key, int TableSize)
{
return Key % TableSize;
}
HashTable InitializeTable(int TableSize)
{
HashTable H;
int i;
if (TableSize < MinTableSize)
{
printf("Table size too small!");
return NULL;
}
H = (struct HashTbl*)malloc(sizeof(struct HashTbl));
if (NULL == H)
{
printf("H is NULL");
return NULL;
}
H->TableSize = NextPrime(TableSize);
H->TheCells = (Cell*)malloc(sizeof(Cell) * H->TableSize);
if (NULL == H->TheCells)
{
printf("H->TheCells is NULL");
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;
CollisionNum = 0;
CurrentPos = Hash( Key, H->TableSize );
while ( H->TheCells[ CurrentPos ].Info != Empty &&
H->TheCells[ CurrentPos ].Element != Key )
{
CurrentPos += 2 * ++CollisionNum - 1;
if ( CurrentPos >= H->TableSize )
CurrentPos -= H->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 ].Element = Key;
}
}
HashTable Rehash( HashTable H )
{
int i, OldSize;
Cell *OldCells;
OldCells = 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;
}
ElementType Retrieve( Position P, HashTable H )
{
return H->TheCells[ P ].Element;
}
void
DestroyTable( HashTable H )
{
free( H->TheCells );
free( H );
}
int main()
{
system("pause");
return 0;
}