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

时间:2014-03-05 12:28:40

标签: c linux search hash linked-list

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

这些是警告。 非常感谢

1 个答案:

答案 0 :(得分:0)

关于代码的几点评论

你忘记了一些包含

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