如下hash表增删改查代码,为什么hash表的初始化InitHashTable函数和修改值modify直接传递他们的地址可以,不应该是 传递指针的地址也就是二级指针才能修改吗。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TABLE_SIZE 100
/*hash 节点*/
typedef struct HashNode
{
char *key;
int value;
struct HashNode *next;
}HashNode;
/*hash 整个表,有一个数组存放了每个由节点自称的链表的位置*/
typedef struct HashTable
{
HashNode *hashNode[MAX_TABLE_SIZE];
int currentIndex;
}HashTable;
/*初始化整个表*/
void InitHashTable(HashTable *hashTable)
{
memset(hashTable->hashNode, 0, MAX_TABLE_SIZE * sizeof(HashNode *));
hashTable->currentIndex = 0;
}
/*一个规则:key转换成一个long型数据,用于确定存放在表中的那个位置*/
unsigned long HashFun(const char *key)
{
unsigned long hash = 0;
int len = strlen(key);
int i;
for (i = 0; i < len; i++)
{
hash = hash * 33 + key[i];
}
return hash;
}
/*插入一个新的key:value数据*/
void Insert(HashTable *hashTable, char *key, int value)
{
int pos = HashFun(key) % MAX_TABLE_SIZE;//通过计算的key转化的值与表的大小取余,来确定存储的位置
/*下面是创建个新节点,把key和value都存在这个节点中*/
HashNode *newNode = (HashNode *)malloc(sizeof(HashNode));
newNode->next = NULL;
newNode->key = (char *)malloc((strlen(key) + 1) * sizeof(char));
strcpy(newNode->key, key);
newNode->key[strlen(key)] = '\0';
newNode->value = value;
/*把新结点插入到表中去*/
HashNode *p = hashTable->hashNode[pos];
if (p == NULL)//
{
hashTable->hashNode[pos] = newNode;
hashTable->currentIndex++;
return ;
}
/*key的值已成存在,就覆盖写入value*/
if (strcmp(p->key, key) == 0)
{
p->value = value;
return;
}
/*通过下面,把节点查到第二个节点的位置(头插法),这里对节点的顺序没有要求*/
newNode->next = p->next;
p->next = newNode;
return ;
}
/*打印表中的信息*/
void PrintHashTable(HashTable *hashTable)
{
int i;
for (i = 0; i < MAX_TABLE_SIZE; i++)
{
HashNode *head = hashTable->hashNode[i];
if (head == NULL)
{
continue;
}
printf("\n数组下标:%d ==>", i);
printf("(%s:%d)", head->key, head->value);
head = head->next;
while (head)
{
printf("-->(%s:%d)", head->key, head->value);
head = head->next;
}
}
printf("\n");
return;
}
void clearHashTable(HashTable *hashTable)
{
int i;
for (i = 0; i < MAX_TABLE_SIZE; i++)
{
HashNode *head = hashTable->hashNode[i];
while (head)
{
HashNode *temp = head->next;
free(head->key);
head->key = NULL;
free(head);
head = NULL;
head = temp;
}
}
return;
}
int modify(HashTable *hashTable, char *key, int value)
{
int pos = HashFun(key) % MAX_TABLE_SIZE;
HashNode *head = hashTable->hashNode[pos];
if (head == NULL)
{
return 1;
}
else
{
HashNode *q = head;
while (q)
{
if (strcmp(q->key, key) == 0)
{
q->value = value;
return 0;
}
}
return 1;
}
}
int main(int argc, char const *argv[])
{
HashTable *hashTable = (HashTable *)malloc(sizeof(HashTable));
InitHashTable(hashTable);
Insert(hashTable, "tgb", 5);
Insert(hashTable, "yhn", 6);
Insert(hashTable, "ujm", 7);
PrintHashTable(hashTable);
modify(hashTable, "c", 8080);//为何 传递一级指针也可以修改
// modify(&hashTable, "c", 8080);//
PrintHashTable(hashTable);
clearHashTable(hashTable);
free(hashTable);
return 0;
}