程序填空题:创建哈希表(拉链法)
创建哈希表,用拉链法解决冲突构造散列表,输出平均查找长度。
```c++
#include
using namespace std;
#define P 13
typedef struct HashNode{
int key;
struct HashNode *next;
}HashNode,* HashTable;
void CreateHash(HashTable HT[],int n){
int j,key;
for(j=1;j<=n;j++){
cin >> key;
int H0=key%P;
HashTable s=new HashNode;
s->key=key;
@@[s->next=HT[H0]](2);
@@[HT[H0]=s](2);
}
}
float ASL(HashTable HT[])
{
float sum=0;
int i,j,cnt=0;
for(i=0;i
```c++
#include
using namespace std;
#define P 13
typedef struct HashNode{
int key;
struct HashNode *next;
}HashNode,* HashTable;
void CreateHash(HashTable HT[],int n){
int j,key;
for(j=1;j<=n;j++){
cin >> key;
int H0=key%P;
HashTable s=new HashNode;
s->key=key;
@@[s->next=HT[H0]](2);
@@[HT[H0]=s](2);
}
}
float ASL(HashTable HT[])
{
float sum=0;
int i,j,cnt=0;
for(i=0;i
{
HashTable s=HT[i];
j=0;
while(s)
{
j++;cnt++;
@@[sum+=j](2);
s=s->next;
}
}
return sum/cnt;
}
int main()
{
int i,n;
HashTable HT[P];
for(i=0;i
HT[i]=NULL;
cin >> n;
CreateHash(HT,n);
cout << ASL(HT);
return 0;
}
```
答案:
第1空:s->next=HT[H0]
第2空:HT[H0]=s
第3空:sum+=j