The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

简单的哈希查找

//除留余数法实现HASH查找
#include<iostream>
#include
<cmath>
using namespace std;

#define HASHSIZE 10000000

struct node
{
    
    
int data;
    node 
*next;
}
hashtable[HASHSIZE];

void initial()

{
    
    
int i;
    
for(i=0;i<HASHSIZE;i++)
    
{
        
        hashtable[i].data
=-1;
        hashtable[i].next
=NULL;
    }

}


void insert(int n)
{
    node 
*p=&hashtable[n%HASHSIZE];
    node 
*q=new node;
    q
->data=n;
    q
->next=p->next;
    p
->next=q;
}



node 
*search(int n)
{
    node 
*p=hashtable[n%HASHSIZE].next;
    
while(p!=NULL)
    
{

        
if(p->data==n)
            
return p;
    }

    
return NULL;
}



int main ()
{
    cout
<<"请输入数据,并以0结束"<<endl;
    
int temp;
    
int i;
    
for(i=1;;i++)
    
{
        cin
>>temp;
        
if(temp==0)
            
break;
        insert(temp);
    }

    cout
<<"请输入要查询的数据:";
    cin
>>temp;
    node 
*p=search(temp);
    
if(p==NULL)
        cout
<<"没有这个数据"<<endl;
    
else 
        cout
<<p->data<<endl;
    system(
"pause");
    
return 0;
}

posted on 2009-05-02 16:39 abilitytao 阅读(622) 评论(1)  编辑 收藏 引用

评论

# re: 简单的哈希查找 2009-05-04 19:55 shongbee2

谢谢,博主,我也正在学HASH。哈哈。。加油。。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理