首页 > 技术文章 > 哈希表---线性探测再散列(hash)

yspworld 2014-12-04 21:26 原文

//哈希表---线性探测再散列

#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#define m 10000
#define NULLkey -1


using namespace std;

int HashTable[m];

int Hash_search( int k)
{
    int p0, pi;
    p0=hash(k); //函数运算值
    if(HashTable[p0]== NULLkey )
    {
        return -1;
    }
    else if(HashTable[p0]==k )
    {
        return p0;
    }
    else //用线性探测再散列 解决冲突
    {
        for(i=0; i<=m-1; i++)
        {
            pi=(p0+i)%m;
            if(HashTable[pi]==NULLkey )
            {
                return -1;
            }
            else if(HashTable[pi]==k )
            {
                return pi;
            }
        }
        return -1;
    }
}

 

推荐阅读