An invalid implementation searches for a record using hash tables. No results found?

I am trying to find an array of struct one record with one field ( id). The code compiles, but does not work. I think this is the problem of creating a hash table. But I can’t fix the problem, I start with hash tables, is it ok to create a hash table of size numbers? Anyway, the access time is O (1) , so I only use a lot of memory right?

Here is my full code, thanks in advance for every tip / comment.

This is my adt:

typedef struct _SPerson {
    char name[20];
    char surname[20];
    char id[20];
    char telephone[20];
} SPerson;

typedef struct SInfo {
    int key;
    SPerson value;
} TInfo;

struct SNode_list {    /*list node*/
    TInfo information;          /*data->key and value*/                            
    struct LNode *next; /*pointer to next element*/
};

typedef struct SNode_list LNode;    // LNode single element of list 
typedef LNode *List;

/*hash table struct*/
typedef struct _HashTable {
    int bucket_number;
    List *bucket;
} HashTable;

I have an array of 20,000 entries loaded in SPerson Archive[20000];

In my data retrieval function, I do this:

void search_using_hash_table(SPerson Archive[], int ne) {
    HashTable *ht = hashtable_create(ne);
    SPerson *app_record
    char *app = get_string();
    for (i = 0; i < ne; i++)       /*Creating Hash-Table*/
        hashtable_insert(ht, i, Archive[i]);
    app_record = hashtable_search(ht, app);
    if (app != NULL)
        printf("\n\nFound.\n %s %s %s %s\n",
               app->id, app->surname, app->name, app->telephone);
    else
        printf("\n\nNot found.\n");
}

These are my functions:

Embed

void hashtable_insert(HashTable *ht, int key, SPerson value) {
    TInfo info;
    LNode *node;
    unsigned h;
    info.key = key;
    info.value = value;
    h = hash(value.id) % ht->bucket_number;

    node = list_search_unordered(ht->bucket[h], info);  

    if (node == NULL)   /*no collision*/
        ht->bucket[h] = list_insert_at_index(ht->bucket[h], info);
    else                /*push*/
        node->information = info;
}                       

Search

SPerson *hashtable_search(HashTable *ht, char *key) {
    unsigned h = hash(key) % ht->bucket_number;      
    TInfo info;
    info.key = key;
    LNode *node = list_search_unordered(ht->bucket[h], info);   
    if (node == NULL)
        return NULL;                                             
    else
        return &node->information.value;
}

List search

LNode *list_search_unordered(List list, TInfo info) {
    LNode *curr;
    curr = list;

    while ((curr != NULL) && !equal(info, curr->information)) {
        curr = curr->next;
    }
    if (curr == NULL)
        return NULL;
    else
        return curr;
}

Equal

bool equal(TInfo a, TInfo b) {
    return a.key == b.key;
}

Create

HashTable *hashtable_create(int buckets) {
    int i;
    HashTable *p = (HashTable *)malloc(sizeof(HashTable));  /*allocation*/
    assert(p != NULL);      /*assert verificate if allocation went ok*/
    assert(buckets > 0);        

    p->bucket_number = buckets;
    p->bucket = (List *)malloc(sizeof(List)*buckets);   
    assert(p->bucket != NULL);

    for (i = 0; i < buckets; i++)
        p->bucket[i] = NULL;    

    return p;
}

Hash algorithm

unsigned long hash(unsigned char *str) { /*djb algorithm*/
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}
+4
1

ids , key , , .

 void hashtable_insert(HashTable *ht, int key, SPerson value)
  {
   TInfo info;
   LNode *node;
   unsigned h;
   info.key = key;
   info.value = value;
   h = hash(value.id) % ht->bucket_number;...

 SPerson *hashtable_search(HashTable *ht, char* key)
 {
   unsigned h = hash(key) % ht->bucket_number;       
   TInfo info;
   info.key = key;
   LNode *node = list_search_unordered(ht->bucket[h], info);...
+3

Source: https://habr.com/ru/post/1628421/


All Articles