本文为散列表(哈希表)的源代码,如若想看算法具体介绍请点击下面链接:
http://blog.csdn.net/aytfly/article/details/8986920
/**
* @brief Hash Table
* @author An
* @data 2013.5.28
**/
#include <iostream>
#define NIL -1
#define tableSize 11
using namespace std;
struct Node
{
int key;
int otherData;
};
typedef Node HashTable[ tableSize ];
// 线性探查
int HashLinear( int k, int i )
{
return ( k + i ) % tableSize;
}
// 二次探查,c1 = 1, c2 = 3
int HashQuadratic( int k, int i )
{
int c1 = 1, c2 = 3;
return ( k + c1 * i + c2 * i * i ) % tableSize;
}
// 双重散列
int HashFirst( int k )
{
return k % tableSize;
}
int HashSecond( int k )
{
return 1 + k % ( tableSize - 1 );
}
int HashDouble( int k, int i )
{
return ( HashFirst( k ) + i * HashSecond( k ) ) % tableSize;
}
// 查找
int HashSearchLinear( HashTable T, int k, int &pos )
{
int i = 0;
do
{
pos = HashLinear( k, i );
if ( T[ pos ].key == k )
return 0;
if ( T[ pos ].key == NIL )
return 1;
} while ( ++i < tableSize );
return -1;
}
int HashSearchQuadratic( HashTable T, int k, int &pos )
{
int i = 0;
do
{
pos = HashQuadratic( k, i );
if ( T[ pos ].key == k )
return 0;
if ( T[ pos ].key == NIL )
return 1;
} while ( ++i < tableSize );
return -1;
}
int HashSearchDouble( HashTable T, int k, int &pos )
{
int i = 0;
do
{
pos = HashDouble( k, i );
if ( T[ pos ].key == k )
return 0;
if ( T[ pos ].key == NIL )
return 1;
} while ( ++i < tableSize );
return -1;
}
// 插入
void HashInsertLinear( HashTable T, int k )
{
int pos, sgn;
sgn = HashSearchLinear( T, k, pos );
if ( sgn == 1 )
T[ pos ].key = k;
else if ( sgn == 0 )
cout << "Duplicate key!" << endl;
else
cout << "The table is full!" << endl;
}
void HashInsertQuadratic( HashTable T, int k )
{
int pos, sgn;
sgn = HashSearchQuadratic( T, k, pos );
if ( sgn == 1 )
T[ pos ].key = k;
else if ( sgn == 0 )
cout << "Duplicate key!" << endl;
else
cout << "The table is full!" << endl;
}
void HashInsertDouble( HashTable T, int k )
{
int pos, sgn;
sgn = HashSearchDouble( T, k, pos );
if ( sgn == 1 )
T[ pos ].key = k;
else if ( sgn == 0 )
cout << "Duplicate key!" << endl;
else
cout << "The table is full!" << endl;
}
// 建表
void CreatHashTableLinear( HashTable T, int *array, int length )
{
if ( length > tableSize )
{
cout << "Error: the table can not load all the elements!" << endl;
return ;
}
for ( int i = 0; i < tableSize; ++i )
{
T[ i ].key = NIL;
}
for ( int i = 0; i < length; ++i )
{
HashInsertLinear( T, array[ i ] );
}
}
void CreatHashTableQuadratic( HashTable T, int *array, int length )
{
if ( length > tableSize )
{
cout << "Error: the table can not load all the elements!" << endl;
return ;
}
for ( int i = 0; i < tableSize; ++i )
{
T[ i ].key = NIL;
}
for ( int i = 0; i < length; ++i )
{
HashInsertQuadratic( T, array[ i ] );
}
}
void CreatHashTableDouble( HashTable T, int *array, int length )
{
if ( length > tableSize )
{
cout << "Error: the table can not load all the elements!" << endl;
return ;
}
for ( int i = 0; i < tableSize; ++i )
{
T[ i ].key = NIL;
}
for ( int i = 0; i < length; ++i )
{
HashInsertDouble( T, array[ i ] );
}
}
void PrintTable( HashTable T )
{
for ( int i = 0; i < tableSize; ++i )
cout << T[ i ].key << " ";
cout << endl;
}
int main()
{
int array[] = { 10, 22, 31, 4, 15, 28, 17, 88, 59 };
int length = sizeof( array ) / sizeof( array[0] );
HashTable T1, T2, T3;
CreatHashTableLinear( T1, array, length );
CreatHashTableQuadratic( T2, array, length );
CreatHashTableDouble( T3, array, length );
PrintTable( T1 );
PrintTable( T2 );
PrintTable( T3 );
return 0;
}