gpt4 book ai didi

c - 使用C linux在链表中进行哈希搜索

转载 作者:行者123 更新时间:2023-11-30 17:38:11 24 4
gpt4 key购买 nike

嗨,我正在使用哈希搜索来搜索链接列表。我的链表包含大约 3500 个随机数,其值范围为 10000 - 1000000。我以前从未使用过哈希搜索,但这次我被分配了这个任务。所以我必须这么做。请查看我的代码并纠正我。虽然有点长,但是拜托,因为我现在非常需要它。从过去 6-7 个小时以来我一直在尝试这样做。

struct entry_s 
{
int *key;
int *value;
struct entry_s *next1;
};

typedef struct entry_s entry_t;

struct hashtable_s
{
int size;
struct entry_s **table;
};

typedef struct hashtable_s hashtable_t;


/* Create a new hashtable. */
hashtable_t *ht_create( int size )
{
hashtable_t *hashtable = NULL;
int i;
if( size < 1 )
return NULL;

/* Allocate the table itself. */
if( ( hashtable = malloc( sizeof( hashtable_t ) ) ) == NULL )
{
return NULL;
}

/* Allocate pointers to the head nodes. */
if( ( hashtable->table = malloc( sizeof( entry_t * ) * size ) ) == NULL )
return NULL;

for( i = 0; i < size; i++ )
{
hashtable->table[i] = NULL;
}
hashtable->size = size;
return hashtable;
}
/* Hash a string for a particular hash table. */
int ht_hash( hashtable_t *hashtable, int *key )
{
unsigned long int hashval;
static int i = 0;
hashval += i;
return hashval % hashtable->size;
}

/* Create a key-value pair. */
entry_t *ht_newpair( int *key, int *value )
{
entry_t *newpair;

if( ( newpair = malloc( sizeof( entry_t ) ) ) == NULL )
return NULL;
if( ( newpair->key = key ) == NULL )
return NULL;
if( ( newpair->value =value ) == NULL )
return NULL;
newpair->next1 = NULL;
return newpair;
}

/* Insert a key-value pair into a hash table. */
void ht_set( hashtable_t *hashtable, int *key, int *value )
{
int bin = 0;
entry_t *newpair = NULL;
entry_t *next1 = NULL;
entry_t *last = NULL;

bin = ht_hash( hashtable, key );
next1 = hashtable->table[ bin ];
while( next1 != NULL && next1->key != NULL && key==next1->key > 0 )
{
last = next1;
next1 = next1->next1;
}

/* There's already a pair. Let's replace that string. */
if( next1 != NULL && next1->key != NULL && key== next1->key == 0 )
{
free( next1->value );
next1->value =value;
}
else
{
newpair = ht_newpair( key, value );
if( next1 == hashtable->table[ bin ] )
{
newpair->next1 = next1;
hashtable->table[ bin ] = newpair;
}
else if ( next1 == NULL )
{
last->next1 = newpair;
}
else
{
newpair->next1 = next1;
last->next1 = newpair;
}
}
}

/* Retrieve a key-value pair from a hash table. */
char *ht_get( hashtable_t *hashtable, int *key )
{
int bin = 0;
entry_t *pair;

bin = ht_hash( hashtable, key );

/* Step through the bin, looking for our value. */
pair = hashtable->table[ bin ];
while( pair != NULL && pair->key != NULL && key==pair->key > 0 )
{
pair = pair->next1;
}

/* Did we actually find anything? */
if( pair == NULL || pair->key == NULL || key==pair->key != 0 )
return NULL;
else
return pair->value;
}

int main()
{
int i;
hashtable_t *hashtable=ht_create(4000);
insert1();
for(cur=first;cur!=last->link;cur=cur->link)
{
ht_set(hashtable,i,cur->data);
i++;
}
printf("Data inserted into the hash table");
}

insert1() 从文本文件中获取数据并将其放入列表中。

fsll.c: In function ‘ht_create’:
fsll.c:55: warning: incompatible implicit declaration of built-in function ‘malloc’
fsll.c: In function ‘ht_newpair’:
fsll.c:88: warning: incompatible implicit declaration of built-in function ‘malloc’
fsll.c: In function ‘ht_set’:
fsll.c:116: warning: passing argument 2 of ‘ht_hash’ makes pointer from integer without a cast
fsll.c:125: warning: comparison between pointer and integer
fsll.c:128: warning: assignment makes pointer from integer without a cast
fsll.c:134: warning: passing argument 1 of ‘ht_newpair’ makes pointer from integer without a cast
fsll.c:134: warning: passing argument 2 of ‘ht_newpair’ makes pointer from integer without a cast
fsll.c: In function ‘ht_get’:
fsll.c:162: warning: passing argument 2 of ‘ht_hash’ makes pointer from integer without a cast
fsll.c:172: warning: comparison between pointer and integer
fsll.c:175: warning: return from incompatible pointer type
fsll.c: In function ‘insert1’:
fsll.c:190: warning: incompatible implicit declaration of built-in function ‘malloc’

这些是警告。非常感谢

最佳答案

关于代码的一些注释

你忘记了一些内容

#include <stdlib.h> //for malloc
#include <stdio.h> // for printf

您的函数中有未初始化的值,这意味着这些值是未知的,并且取决于创建时存储在内存中的内容,因此它们会递增,结果仍然未知。

unsigned long int hashval;
int i;

在 ht_set 中

while( pair != NULL && pair->key != NULL && key==pair->key > 0 )
if( pair == NULL || pair->key == NULL || key==pair->key != 0 )

我不明白 key==pair->key > 0key==pair->key != 0,也许你的意思是

while( pair != NULL && pair->key != NULL && key==pair->key)
if( pair == NULL || pair->key == NULL || key==pair->key)

在主目录

也许我缺少一些代码,但我没有看到 cur,first,last 的声明。此外,您的 ht_create 函数似乎将所有条目设置为 NULL,因此,不能有下一个。我不认为一切都在那里,因为我没有所有代码,我无法编译它并尝试运行它。

关于c - 使用C linux在链表中进行哈希搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22197806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com