ADD file via upload
This commit is contained in:
parent
50baad8e48
commit
f92554b0f4
|
@ -0,0 +1,160 @@
|
|||
//拉链法 建立长度为n的哈希表并解决哈希冲突
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<time.h>
|
||||
#include<test_hash.h>
|
||||
#define N -32768
|
||||
#define max 10
|
||||
|
||||
//结点结构体
|
||||
typedef struct LNode
|
||||
{
|
||||
int data;//存储值
|
||||
struct LNode *next;
|
||||
}LNode,*Linklist;
|
||||
|
||||
//构建哈希表结构体
|
||||
typedef struct HashTable
|
||||
{
|
||||
Linklist list;
|
||||
int count;//记录该结点下共发生冲突结点个数
|
||||
}HashTable;
|
||||
|
||||
//哈希函数 计算
|
||||
int Hash(int key)
|
||||
{
|
||||
return key % 5;
|
||||
}
|
||||
|
||||
//初始化哈希表函数
|
||||
void InitHashTable (HashTable *h)
|
||||
{
|
||||
//初始化链表表头
|
||||
h->list=(LNode *)malloc(max * sizeof(LNode));
|
||||
h->count=0;
|
||||
|
||||
//初始化
|
||||
for(int i=0;i<max;i++)
|
||||
{
|
||||
h->list[i].data=N;
|
||||
h->list[i].next=NULL;
|
||||
}
|
||||
printf("哈希表已初始化\n");
|
||||
}
|
||||
|
||||
//关键字插入函数
|
||||
void InsertKey(HashTable *h,int key,int i)
|
||||
{
|
||||
//获取下标
|
||||
int id=Hash(key);
|
||||
printf("\n插入关键字%d,哈希地址=%d\n",key,id);
|
||||
//判断是否插入关键字
|
||||
if(h->list[id].data==N)
|
||||
{
|
||||
//未发生冲突
|
||||
h->list[id].data=key;
|
||||
printf("第%d个数已插入\n",i+1,id);
|
||||
}
|
||||
else
|
||||
{
|
||||
//发生冲突,插入新结点
|
||||
LNode *node;
|
||||
node=(LNode *)malloc(sizeof(LNode));
|
||||
node->data=key;
|
||||
node->next=h->list[id].next;
|
||||
h->list[id].next=node;
|
||||
printf("发生冲突,第%d个数已插入\n",i+1,id);
|
||||
}
|
||||
h->count++;
|
||||
}
|
||||
|
||||
//关键字查找
|
||||
LNode *Search(HashTable h,int key)
|
||||
{
|
||||
int id=Hash(key);
|
||||
LNode *head=&h.list[id];//获取头结点
|
||||
while(head!=NULL&&head->data!=key)//找到关键字对应结点
|
||||
{
|
||||
head=head->next;
|
||||
}
|
||||
return head;
|
||||
|
||||
}
|
||||
|
||||
//打印哈希表
|
||||
void show(HashTable h)
|
||||
{
|
||||
for(int i=0;i<5;i++)
|
||||
{
|
||||
printf("%d\t",i);
|
||||
}
|
||||
printf("\n");
|
||||
LNode *node;
|
||||
int j=0;
|
||||
int count=0;
|
||||
int flag=1;
|
||||
while(flag)
|
||||
{
|
||||
flag=0;
|
||||
for(int i=0;i<max;i++)
|
||||
{
|
||||
j=0;
|
||||
node=&h.list[i];
|
||||
while(j<count&&node)
|
||||
{
|
||||
node=node->next;
|
||||
j++;
|
||||
}
|
||||
if(node&&node->data!=N)
|
||||
{
|
||||
printf("%d\t",node->data);
|
||||
flag=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" \t");
|
||||
}
|
||||
}
|
||||
count++;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
system("chcp 65001");
|
||||
int key;//关键字
|
||||
HashTable h;
|
||||
InitHashTable(&h);
|
||||
printf("输入关键字构建哈希表:\n");
|
||||
srand((unsigned)time(NULL));
|
||||
for(int i=0;i<max;i++)
|
||||
{
|
||||
// scanf("%d",&key);
|
||||
key=rand()%100;
|
||||
printf("已输入关键字:%d",key);
|
||||
// key=i;
|
||||
InsertKey(&h,key,i);
|
||||
}
|
||||
show(h);
|
||||
int flag=1;
|
||||
int temp=3;
|
||||
while(flag)
|
||||
{
|
||||
LNode *node;
|
||||
key=rand()%100;
|
||||
printf("查找关键字:%d",key);
|
||||
|
||||
// scanf("%d",&key);
|
||||
node=Search(h,key);
|
||||
if(node!=NULL)
|
||||
{
|
||||
printf("查找成功!!!\n");
|
||||
temp--;
|
||||
}
|
||||
|
||||
else printf("查找失败\n");
|
||||
if(temp==0) break;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue