您的位置:首页 > 其它

哈希表之除留余数法+线性探测法,链地址法,公共溢出区法

2016-08-01 16:09 447 查看
线性探测法

#include<iostream>
using namespace std;
#define HASHSIZE 10
#define NULLKEY -32768
typedef struct hash
{
int *element;
}HashTable;
void Init(HashTable *p)
{
p->element = (int*)malloc(sizeof(int)*HASHSIZE);
for (int i = 0; i < HASHSIZE; ++i)
p->element[i] = NULLKEY;

}
// 除留余数法
int HASH(int key)
{
return key%HASHSIZE;
}
//插入元素
void InsertHash(HashTable *p, int key)
{
int addr = HASH(key);
while (p->element[addr] != NULLKEY) //处理冲突, 使用开放地址法之 线性探测法
addr = (addr + 1) % HASHSIZE;
p->element[addr] = key;
}
int Search(HashTable *p, int key)
{
int addr = HASH(key);
if (key == p->element[addr])
return addr;
else
{
addr = (addr + 1) % HASHSIZE;
while (key != p->element[addr] && p->element[addr] != NULLKEY&&addr!=HASH(key))
addr = (addr + 1) % HASHSIZE;
if (key == p->element[addr])
return addr;
}
return -1;
}
int main(void)
{
int a[10] = { 12,45,2,6,78,9,0,1,15 };
HashTable table;
Init(&table);
for (int i = 0; i < 10; i++)
InsertHash(&table, a[i]);

int res = Search(&table, 15);
if (res!=-1)
cout << "查找成功!地址为: "<<res<<endl;
else
cout << "查找失败! "<<endl;
system("pause");
return 0;
}


链地址法
#include<iostream>
using namespace std;
#define HASHSIZE 10
typedef struct node
{
int num;
struct node *next;
}NODE;
typedef struct hash
{
NODE *element[HASHSIZE];
int count;
}HashTable;
void Init(HashTable *p)
{
for (int i = 0; i < HASHSIZE; ++i)
{
p->element[i] = (NODE*)malloc(sizeof(NODE)*HASHSIZE);
p->element[i]->next = NULL;
}

}
// 除留余数法
int HASH(int key)
{
return key%HASHSIZE;
}
//插入元素
void InsertHash(HashTable *p, int key)
{
int addr = HASH(key);
//采用链地址法。 头插法
NODE *q = (NODE*)malloc(sizeof(NODE));
q->num = key;
q->next = p->element[addr]->next;
p->element[addr]->next = q;

}
int Search(HashTable *p, int key)
{
int addr = HASH(key);
NODE *m = p->element[addr]->next;
while (m != NULL)
{
if (m->num == key)
return addr;
m = m->next;
}
return -1;
}
int main(void)
{
int a[10] = { 12,45,2,6,78,9,0,1,15 };
HashTable table;
Init(&table);
for (int i = 0; i < 10; i++)
InsertHash(&table, a[i]);

int res = Search(&table, 12);
if (res!=-1)
cout << "查找成功!地址为: "<<res<<endl;
else
cout << "查找失败! "<<endl;
system("pause");
return 0;
}
公共溢出区法
#include<iostream>
using namespace std;
#define HASHSIZE 10
#define NULLKEY -32768
typedef struct hash
{
int *element;
int *extraElement;//溢出区表
int count; // 溢出区的元素个数
}HashTable;
// 由于 通过哈希函数 计算后的地址重复几率很小,所以把重复部分用顺序表来存放,查找时遍历这个公共溢出区的顺序表,效率会更高。
void Init(HashTable *p)
{
p->element = (int*)malloc(sizeof(int)*HASHSIZE);
p->extraElement = (int*)malloc(sizeof(int)*HASHSIZE);
for (int i = 0; i < HASHSIZE; ++i)
{
p->element[i] = NULLKEY;
p->extraElement[i] = NULLKEY;
}
p->count = 0;
}
// 除留余数法
int HASH(int key)
{
return key%HASHSIZE;
}
//插入元素
void InsertHash(HashTable *p, int key)
{
int addr = HASH(key);
//处理冲突, 使用公共溢出法
if (p->element[addr] == NULLKEY)
p->element[addr] = key;
else //如果元素内已经有值了,就放到溢出部分的表中,且把count加 1
p->extraElement[p->count++] = key;
}
int Search(HashTable *p, int key)
{
int addr = HASH(key);
if (key == p->element[addr])
return addr;
else
{
for (int i = 0; i < p->count; ++i)
if (p->extraElement[i] == key)
{
cout << "溢出表" << endl;
return i;
}
}
return -1;
}
int main(void)
{
int a[10] = { 12,45,2,6,78,9,0,1,15 };
HashTable table;
Init(&table);
for (int i = 0; i < 10; i++)
InsertHash(&table, a[i]);

int res = Search(&table, 2);
if (res != -1)
cout << "查找成功!地址为: " << res << endl;
else
cout << "查找失败! " << endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: