📄 hash.c
字号:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define NUM 200
#define B 100
struct Node{
int value;
struct Node* next;
};
struct Bucket{
int value[3];
struct Node* next;
};
struct Bucket bucket[B];
int hash(int v){
return v%100;
}
void init(){
int i,j;
for(i=0;i<B;i++){
for(j=0;j<3;j++)
bucket[i].value[j]=-1;
bucket[i].next=0;
}
}
void print(){
int i,j;
struct Node* node;
for(i=0;i<B;i++){
for(j=0;j<3;j++){
if(bucket[i].value[j]==-1){
printf("-1");
break;
}else printf("%d ",bucket[i].value[j]);
}
node=bucket[i].next;
while(node!=NULL){
printf("%d ",node->value);
node=node->next;
}
printf("\n");
}
}
int main(){
int r,h,i,j;
struct Node* node;
init();
srand(time(0));
for(i=0;i<NUM;i++){
r=rand();
h=hash(r);
for(j=0;j<3;j++){
if(bucket[h].value[j]==-1){
bucket[h].value[j]=r;
break;
}
}
// bucket is full
if(j==3){
node=(struct Node*)malloc(sizeof(struct Node));
node->value=r;
node->next=bucket[h].next;
bucket[h].next=node;
}
}
print();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -