散列表

/***************************
 *文件名:hash.cpp
 *功能:散列表
 *创建日期:2007-9-27
**************************
*/


#include 
< iostream >
using   namespace  std;

const   int  m  =   11 ;

int  h( int  k,  int  i)
{
    
return (k % m + i) % m;
}


int  Insert_hash( int  ar[],  int  k)
{
    
int i, j;

    i 
= 0;
    
while( i < m )
    
{
        j 
= h(k, i);
        
if(ar[j] == -1)
        
{
            ar[j] 
= k;
            
return j;            
        }

        
else
            
++i;
    }

    
    
return -1;
}


void  print( int  a[],  int  n)
{
    
int *= a + n;  // b = a + 4 * n; 因为一个int占四个字节长度
    cout << *<< ' ';
    
while++!= b )
        cout 
<< *<< ' ';
}


//  初始化函数,将数组 a中元素初始化为v, n 为数组大小
void  Init( int  a[], int  n,  int  v)
{
    
int *= a + n;

    
*= v;
    
while++!= b )
        
*= v;
}


int  main()
{
    
int s[] = 10223141528178859 };
    cout 
<< "s : ";
    print( s, 
sizeof(s) / sizeof(s[0]) );
    cout 
<< endl;

    
int hashtable[m];
    Init(hashtable, 
sizeof(hashtable) / sizeof(hashtable[0]), -1);

    
for(int i = 0; i < sizeof(s) / sizeof(s[0]); i++)
        Insert_hash(hashtable, s[i]);

    cout 
<< "hashtable : ";
    print( hashtable, 
sizeof(hashtable) / sizeof(hashtable[0]) );
    cout 
<< endl;

    
return 0;

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值