哈希表之除留余数法+线性探测法,链地址法,公共溢出区法
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;
}
#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;
}
相关文章推荐
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- 哈希表——线性探测法、链地址法、查找成功、查找不成功的平均长度
- 哈希表——线性探测法、链地址法、查找成功、查找不成功的平均长度
- 哈希表——线性探测法、链地址法、查找成功、查找不成功的平均长度
- 哈希表——线性探测法、链地址法、查找成功、查找不成功的平均长度
- 【除留余数法定义hash函数+线性探测法解决hash冲突】数据结构实验之查找七:线性之哈希表
- 哈希表——线性探测法、链地址法、查找成功、查找不成功的平均长度
- C++ 哈希表 线性探测 二次探测 哈希桶
- 线性探测法构造哈希表(hash)
- 哈希表的构造之线性探测法
- 哈希表-开放地址法之线性探测代码(JAVA)
- 哈希表开放寻址法之线性探测法解决冲突问题
- 线性探测法构造哈希表(hash)
- 链地址法和线性探测法求查找成功与不成功的平均查找长度ASL
- 哈希表-线性探测
- 线性探测法构造哈希表(hash)
- 搜索结构之哈希表(线性探测法)
- 线性探测法构造哈希表(hash)
- 实验 哈希表线性探测再散列