【问题标题】:hash search in linked list using C linux使用C linux在链表中进行哈希搜索
【发布时间】:2014-04-07 12:20:18
【问题描述】:

您好,我正在使用哈希搜索来搜索链接列表。我的链表包含大约 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’

这些是警告。 非常感谢

【问题讨论】:

  • 您有具体的问题和/或疑问吗?代码是否有效并且您要求改进? ...?
  • 不,代码不起作用。它正在编译并给出几个警告。并且在将数据插入链表后出现分段错误
  • 那么请显示cour编译器警告并使用调试器检测segfault的行。
  • 'ht_set' 传递'ht_hash' 的参数 2 使指针从整数而不在指针和整数之间进行强制比较
  • 对不起,我的意思是:通过将它们放入您的问题、代码块、行号和所有内容中,显示您的几个警告的“全部”。还有调试结果。

标签: c linux search hash linked-list


【解决方案1】:

只是对代码的几点说明

你忘记了一些包含

#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-&gt;key &gt; 0key==pair-&gt;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,因此,不可能有下一个。我不认为一切都在那里,因为我没有所有代码,我无法编译它并尝试运行它。

【讨论】:

    猜你喜欢
    • 2014-04-07
    • 2015-07-20
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多