本帖最后由 ing 于 2020-3-5 16:44 编辑
我的运行没有任何结果的输出
我很确定我的代码没错,当我断点调试按下F11时发生了下图
#include<malloc.h>
#include<stdio.h>
#include<stdbool.h>
#define VType int
#define Hash_Size 20
#define NULL_KEY -1
typedef struct
{
int *elem;
int length;
}HashTable;
void init(HashTable *t, int length)
{
t->elem = (int*)malloc(Hash_Size*sizeof(int));
t->length = length;
for (size_t i = 0; i < Hash_Size; i++)
{
t->elem[i] = NULL_KEY;
}
}
int hash(int a)
{
return a % 13;
}
void insert(HashTable **t,int a)
{
int hashAddress = hash(a);
while ((*t)->elem[hashAddress] != NULL_KEY)
{
hashAddress = (hashAddress + 1) % 13;
}
(*t)->elem[hashAddress] = a;
}
int search(HashTable *t, int a)
{
int hashAddress = hash(a);
while (t->elem[hashAddress] != a)
{
hashAddress = (hashAddress + 1) % 13;
if (hashAddress == hash(a))
{
return -1;
}
}
return hashAddress;
}
int main(int argc, char* argv[])
{
HashTable *t = (HashTable*)malloc(Hash_Size*sizeof(HashTable));
init(t,10);
int arr[Hash_Size] = { 12,23,45,57,20,03,78,31,15,36 };
for (size_t i = 0; i < t->length; i++)
{
insert(&t,arr[i]);
}
int result = search(t, 12);
if (result != NULL_KEY)
{
printf("在哈希表的位置是:%d",result);
}
else
printf("哈希表不存在此元素");
free(t->elem);
free(t);
}
|