这是带有链接代码的散列。我在这里有一些指针疑问
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
变成一个数组的,它是一个指向哈希的指针?而且,实际上是什么
struct hash
{
struct node* head;
int count;
};
我不明白这个结构实际上是如何工作的?我们可以将任何指向结构的指针转换为数组吗???