C中的HashTable不适用于字符串

ceid-vg

所以我一直在寻找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!!!
斯蒂芬·莱克纳

请注意 - 正如 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 代码而不是字符)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

函数调用适用于字符串文字,但不适用于字符串变量C

来自分类Dev

字符串匹配不适用于特殊字符“ /”

来自分类Dev

字符串匹配不适用于特殊字符“ /”

来自分类Dev

字符串操作不适用于特殊字符

来自分类Dev

gethash不适用于字符串键

来自分类Dev

子字符串不适用于jquery .text()

来自分类Dev

UpdateResource不适用于lpType作为字符串

来自分类Dev

评估不适用于多行字符串

来自分类Dev

jQuery:包含不适用于html字符串

来自分类Dev

printf不适用于字符串

来自分类Dev

javascript样式不适用于分割的字符串

来自分类Dev

字符串搜索不适用于请求结果

来自分类Dev

List <PdfPCell>不适用于参数(字符串)

来自分类Dev

MySQL函数不适用于unicode字符串

来自分类Dev

子字符串不适用于jquery .text()

来自分类Dev

字符串replaceAll不适用于$

来自分类Dev

sed命令不适用于搜索字符串

来自分类Dev

比较字符串不适用于cin

来自分类Dev

javascript排序不适用于字符串

来自分类Dev

粘贴不适用于长字符串?

来自分类Dev

子字符串函数不适用于变量

来自分类Dev

为什么 C# 编组字符串不适用于 C++ DLL

来自分类Dev

隐式转换适用于符号,但不适用于字符串

来自分类Dev

SQL UDF适用于字符串,不适用于列

来自分类Dev

使用isEqualToString进行比较不适用于iPhone中的Url编码的字符串

来自分类Dev

在Visual Studio2012中,“ <<”操作数不适用于字符串数组

来自分类Dev

字符串比较似乎不适用于从文件中读取的行

来自分类Dev

-1乘以sorted()不适用于python中的字符串数据

来自分类Dev

json中的字符串替换不适用于复杂对象

Related 相关文章

热门标签

归档