// Hash.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<malloc.h>
#define SUCCESS 1
#define UNSUCCESS 0
#define HASHSIZE 12
#define NULLKEY -32768
#include<iostream>
using namespace std;
typedef struct
{
int *elem;
int count;
}HashTable;
int m = 0;
int InitHshTable(HashTable *H)
{
int i;
m = HASHSIZE;
H->count = m;
H->elem = (int *)malloc(m*sizeof(int));
for (i = 0; i < m; i++)
{
H->elem[i] = NULLKEY;
}
return 1;
}
int Hash(int key)
{
return key %m;
}
void InsertHash(HashTable *H, int key)
{
int addr = Hash(key);
while (H->elem[addr] != NULLKEY)
addr = (addr + 1) % m;
H->elem[addr] = key;
}
bool SearchHash(HashTable H, int key, int *addr)
{
*addr = Hash(key);
while (H.elem[*addr] != key)
{
*addr = (*addr + 1) % m;
if (H.elem[*addr] == NULLKEY || *addr == Hash(key))
{
return 0;
}
}
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
int nums[] = { 12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34 };
m = 11;
HashTable H;
InitHshTable(&H);
for (int i = 0; i != 12; i++)
{
InsertHash(&H, nums[i]);
}
system("pause");
return 0;
}
大话数据结构——散列
最新推荐文章于 2025-06-17 21:00:47 发布