用链式c代码进行散列

用链式c代码进行散列,c,algorithm,pointers,hash,C,Algorithm,Pointers,Hash,下面是使用链接代码的哈希。我在这里有些疑问 struct hash* hashTable = NULL; int eleCount = 0; struct node { int key; int age; char name[100]; struct node* next; }; struct hash { struct node* head; int c

下面是使用链接代码的哈希。我在这里有些疑问

struct hash* hashTable = NULL;
int          eleCount  = 0;

struct node
{
    int          key;
    int          age;
    char         name[100];
    struct node* next;
};

struct hash
{
    struct node* head;
    int          count;
};

struct node* createNode(int key, char *name, int age)
{
    struct node* newnode;

    newnode = (struct node *)malloc(sizeof(struct node));

    newnode->key = key;
    newnode->age = age;
    strcpy(newnode->name, name);
    newnode->next = NULL;

    return newnode;
}

void insertToHash(int key, char *name, int age)
{
    int          hashIndex = key % eleCount;
    struct node* newnode   = createNode(key, name, age);

    /* head of list for the bucket with index "hashIndex" */
    if (!hashTable[hashIndex].head)
    {
        hashTable[hashIndex].head  = newnode;
        hashTable[hashIndex].count = 1;

        return;
    }

    /* adding new node to the list */
    newnode->next = (hashTable[hashIndex].head);
    /*
     * update the head of the list and no of
     * nodes in the current bucket
     */
    hashTable[hashIndex].head = newnode;
    hashTable[hashIndex].count++;
    return;
}
hashTable
是如何变成一个数组的?它是指向hash right的指针?? 实际上是什么

struct hash
{
    struct node* head;
    int          count;
};
我不明白这个结构是如何运作的??
我们可以将指向结构的任何指针转换为数组吗?

哈希表实际上是指向结构的指针。但是,在代码中的某个地方(您没有显示),内存被分配给
struct hash
数组。然后将该内存的地址分配给
哈希表
。然后当你做
hashTable[index]您只需访问此分配的阵列

在C语言中,当您编写
哈希表[index]时这与
*(哈希表+索引)
相同。然后,将
index
添加到此指针会产生一个
index*sizeof(struct hash)
地址。这具有“跳转”到数组中的
索引
”元素的效果


要了解更多详细信息,请阅读。

很难弄清楚你在问什么。请澄清一下。这是一个带有链接的哈希表,它与图中邻接列表的数据结构相同。我无法理解struct hash*hashTable如何用作inserttohash函数中的数组指针和数组在C中的行为非常相似。您可以使用
[]
两者都有。哈希表的这种实现是不正确的,我们无法获得O(1)检索时间(即使没有冲突)