【问题标题】:Insert function of Hashtable in CC中Hashtable的插入函数
【发布时间】:2013-07-18 04:46:01
【问题描述】:

所以,我有这些功能。如何在Hashtable 中插入数字? for 直到桌子的大小?我不知道for 里面有什么,如果它存在的话。

#include <stdio.h>

//Structure
typedef struct Element {
    int key;
    int value;
} Element;

typedef struct HashTable {
    Element *table[11];
} HashTable;


//Create an empty Hash
HashTable* createHashTable() {
    HashTable *Raking = malloc(sizeof(HashTable));

    int i;
    for (i = 0; i < 11; i++) {
        Raking->table[i] = NULL;
    }
    return Raking;
}

//Insert element
void insertElement(HashTable *Raking, int key, int value) {

    int h = hashFunction(key);

    while(Raking->table[h] != NULL) {

        if(Raking->table[h]->key == key) {
            Raking->table[h]->value = value;
            break;
        }

        h = (h + 1) % 11;
    }

    if(Raking->table[h] == NULL) {
        Element *newElement = (Element*) malloc(sizeof(Element));
        newElement->key = key;
        newElement->value = value;
        Raking->table[h] = newElement;
    }
}

int main() {

    HashTable * Ranking = createHashTable();


    /** ??? **/


}

有人可以向我解释如何使用这些结构编写我的主要功能吗?在这种情况下,我正在修复此表中的元素数量,对吗? (表 [11])我可以为用户做些什么来确定哈希表的大小?可能吗?还是我应该设置大小?

【问题讨论】:

  • 不是hashFunction(key) 假设是hashFunction(chave)(或者不应该在函数参数中将chave 转换为key)?这编译了吗?
  • 翻译错误,抱歉。已经编辑了!不,还没有编译,因为 main 函数还没有完成。
  • 您在翻译时似乎产生了一些语法错误。请尝试修复它们。您还可以展示哈希函数的实现,它可能很有用。
  • 这是使用线性探测实现的吗?
  • 像什么,@TaylorFlores?又看了一遍,这次没有发现翻译错误。

标签: c data-structures hash hashtable hashcode


【解决方案1】:

我已经为您的代码添加了 cmets 和更改,我认为这些更改会对您有用。我还对其进行了调整,因此大小不是硬编码的。最后我释放了所有malloc-ed 语句。

这个编译没有错误,我已经使用valgrind 测试了它的内存泄漏和其他错误,没有发现任何投诉。

如果有不清楚的地方和 cmets 无法解释,请告诉我。我已尝试尽可能地坚持您的代码,但我没有机会正确测试功能。

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

//Structure
typedef struct Element {
    int key;
    int value;
} Element; /* you had a syntax error here */

typedef struct HashTable {
    int size; /* we will need the size for the traversal */
    Element *table; /* leave it as a pointer */
} HashTable; /* a syntax error here too */

HashTable* createHashTable(int size) {
    HashTable *Ranking = malloc(sizeof(HashTable));

    /* set the pointer to point to a dynamic array of size 'size' */
    /* this way you don't have to hardcode the size */
    Ranking->table = malloc(sizeof(Element) * size); 
    Ranking->size = size;

    /* initialisation is a bit different because we don't have pointers here */
    /* only table is a pointer, not its elements */
     int i;
     for (i = 0; i < size; i++) {
         Ranking->table[i].key = 0; 
         Ranking->table[i].value = 0;
     }

    return Ranking;
}

/* I implemented a fake hashFunction just to test the code */
/* all it does is make sure the key does not exceed the size of the table */
int hashFunction(int key, int size)
{
    return (key % size);
}

//Insert element
void insertElement(HashTable *Ranking, int key, int value) {

    int h = hashFunction(key, Ranking->size);
    int i = 0;

    /* if hash is full and key doesn't exist your previous loop would have gone on forever, I've added a check */
    /* also notice that I check if table[h] has empty key, not if it's null as this is not a pointer */
    while(Ranking->table[h].key != 0 && (i < Ranking->size)) {

        if(Ranking->table[h].key == key) {
            Ranking->table[h].value = value;
            return; /* break is intended to quit the loop, but actually we want to exit the function altogether */
        }

        h = (h + 1) % Ranking->size; /* changed 11 to the size specified */
        i++; /* advance the loop index */
    }

    /* okay found a free slot, store it there */
    if(Ranking->table[h].key == 0) {
        /* we now do direct assignment, no need for pointers */
        Ranking->table[h].key = key;
        Ranking->table[h].value = value;
    }
}

int main() {

    int size = 0;
    scanf(" %d", &size);

    HashTable *Ranking = createHashTable(size);

    insertElement(Ranking, 113, 10); /* this is just a test, 113 will be hashed to be less than size */

    /* we free everything we have malloc'ed */
    free(Ranking->table);
    free(Ranking);

    return 0;
}

【讨论】:

  • 我是否可以建议一个带有原型void destroyHashTable(HashTable*); 的函数与createHashTable(.) 配对,而不是在main() 末尾直接调用free()(增加封装并减少耦合)?另外,请称我为完美主义者,但HashTable* createHashTable(int size) 迫切希望成为HashTable* createHashTable(size_t size)。是的,我确实意识到(在 32 位 int 架构上)您必须为此分配一个 32 Gibibyte 哈希表。我确实说过完美主义者。
猜你喜欢
  • 2015-05-25
  • 1970-01-01
  • 2017-05-02
  • 2018-07-21
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多