C中的HashTable不适用于字符串

时间:2017-04-03 22:10:47

标签: c pointers hash hashmap hashtable

所以我一直在寻找 HashTable 实现,我需要我的代码接受输入而不是整数的字符串,就像大多数具有键值连接的示例一样。我无法弄清楚为什么输出如此奇怪。我无法使用%s打印项目,因为我收到分段错误(代码转储)错误,这就是我在 display()函数中使用%c的原因。

源代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 20
#define SEARCHKEY "10"

struct DataItem
{
   char value;   
   char key;
};

struct DataItem* hashArray[SIZE]; 
struct DataItem* dummyItem;
struct DataItem* item;

struct DataItem *get(char *key)
{
    int hashIndex = 0;      //get the hash 
    while(hashArray[hashIndex] != NULL)     //move in array until an empty 
    {    
        if(hashArray[hashIndex]->key == key)    return hashArray[hashIndex]; 
        ++hashIndex;        //go to next cell
        hashIndex %= SIZE;  //wrap around the table
    }        
    return NULL;        
}

void put(char *key,char *value)
{
    struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
    item->value = *value;  
    item->key = *key;
    int hashIndex = 0;  //get the hash
    bool condition = false;
    while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1)  //move in array until an empty or deleted cell
    {
        if (hashArray[hashIndex]->key == item->key)
        {   
            condition = true;
            hashArray[hashIndex]->value = item->value;
        }
        ++hashIndex;        //go to next cell
        hashIndex %= SIZE;      //wrap around the table
    }
    if (condition == false) hashArray[hashIndex] = item;
}

void display() 
{
    int i = 0;
    printf("\n");
    for(i = 0; i<SIZE; i++) 
    {
        if(hashArray[i] != NULL)    printf("(%c,%c) ",hashArray[i]->key,hashArray[i]->value);
            else    printf(" ~~ ");
    }
    printf("\n\n");
}

int main() 
{
    dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
    dummyItem->value = -1;  
    dummyItem->key = -1; 
    put("1", "20");
    put("2", "30");
    put("4", "40");
    put("8", "50");
    put("10", "60");
    put("10", "60");
    display();
    item = get(SEARCHKEY); // key search
    printf("Searching for key %s...\n", SEARCHKEY);
    if(item != NULL)    printf("Element found: %d\n", item->value);
        else        printf("No element found!!!\n");
}

终端输出:

$ gcc -o tcphash tcphash.c
tcphash.c: In function ‘get’:
tcphash.c:23:38: warning: comparison between pointer and integer
         if(hashArray[hashIndex]->key == key) return hashArray[hashIndex]; 
                                      ^
$ ./tcphash

(1,6) (2,3) (4,4) (8,5)  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~  ~~ 

Searching for key 10...
No element found!!!

1 个答案:

答案 0 :(得分:1)

请注意 - 正如John Bollinger所述 - 您的数据结构存储单个字符而不是字符串;因此,您的函数分别仅存储和打印键和值的第一个字符。无论如何,有两个问题:

首先,将if(hashArray[hashIndex]->key == key)替换为if(hashArray[hashIndex]->key == *key)。否则,您将字符值与指针值进行比较,这可能会让函数选择错误的项目或根本没有项目。

其次,将printf("Element found: %d\n", item->value)替换为printf("Element found: %c\n", item->value),否则您将打印一个字符值作为整数值(实际上是未定义的行为,但可能会打印ascii代码而不是字符)。