首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

哈希链式解决冲突异常

2012-09-18 
哈希链式解决冲突错误运行时错误大家帮我看看哪出问题了我找了好久都没找到#includeiostream#include c

哈希链式解决冲突错误
运行时错误 大家帮我看看哪出问题了 我找了好久都没找到
#include<iostream>
#include <cstdlib>
using namespace std;
struct link
{
int data;
link *next;
};
class linkhash
{
public:
link lk[10];
public:
linkhash()
{
for(int i=0;i<10;i++)
{
lk[i].data=0;
lk[i].data=NULL;
}
}
void setlink(int num)//哈希存储
{
link *p;
link *q;
int n;
n=num%10;
if(lk[n].data==0)
{
lk[n].data=num;

}
else
{
p=new link;
if(p==NULL)
exit(1);
q=&lk[n];
while(q->next!=NULL)
q=q->next;
p->data=num;
p->next=NULL;
q->next=p;
q=p;
}
}

bool seach(int n)
{
int flag=n%10;
if(lk[flag].data==n)
return 1;
else
{
link *qq;
qq=lk[flag].next;
while(qq!=NULL)
{
if(qq->data==n)
return 1;
qq=qq->next;
}

}
return 0;
}
};
int main()
{
linkhash lkhs;
for(int i=0;i<10;i++)
{
lkhs.setlink(rand()%100);
cout<<lkhs.lk[i].data<<" ";
}
cout<<endl;
if(lkhs.seach(41))
cout<<"the number which you seach was int the table"<<endl;
else
cout<<"seach Failure"<<endl;
return 0;
}

[解决办法]
for(int i=0;i<10;i++)
{
lk[i].data=0;
lk[i].next=NULL;
}
[解决办法]
吃一堑长一智!!!!!!!!!!

热点排行