int insertHash(HashTable* hashTable, int key, int value) {
if (hashTable == NULL) {
printf("哈希表为空,无法插入\n");
return 0;
}
int hashIndex = hashFunction(hashTable, key);
HashNode* curNode = hashTable->table[hashIndex];
while (curNode != NULL) {
if (curNode->key == key) {
curNode->value = value;
printf("更新键:%d\n", key);
return 2;
}
curNode = curNode->next;
}
HashNode* newNode = (HashNode*)malloc(sizeof(HashNode));
if (newNode == NULL) {
printf("内存分配失败\n");
return 0;
}
newNode->key = key;
newNode->value = value;
newNode->next = hashTable->table[hashIndex];
hashTable->table[hashIndex] = newNode;
hashTable->count++;
printf("插入成功:键=%d, 值=%d\n", key, value);
return 1;
}
int searchHash(HashTable* hashTable, int key, int* value) {
if (hashTable == NULL || value == NULL) {
return 0;
}
int hashIndex = hashFunction(hashTable, key);
HashNode* curNode = hashTable->table[hashIndex];
while (curNode != NULL) {
if (curNode->key == key) {
*value = curNode->value;
return 1;
}
curNode = curNode->next;
}
return 0;
}
int DeleteHash(HashTable* hashTable, int key) {
if (hashTable == NULL) {
printf("哈希表为空,无法删除\n");
return 0;
}
int hashIndex = hashFunction(hashTable, key);
HashNode* curNode = hashTable->table[hashIndex];
HashNode* prevNode = NULL;
while (curNode != NULL) {
if (curNode->key == key) {
if (prevNode == NULL) {
hashTable->table[hashIndex] = curNode->next;
} else {
prevNode->next = curNode->next;
}
free(curNode);
hashTable->count--;
printf("删除成功\n");
return 1;
}
prevNode = curNode;
curNode = curNode->next;
}
printf("删除失败\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct HashNode {
int key;
int value;
struct HashNode* next;
} HashNode;
typedef struct {
HashNode** table;
int size;
int count;
} HashTable;
HashTable* createHashTable(int size) {
if (size <= 0) {
printf("哈希表大小必须大于0\n");
return NULL;
}
HashTable* hashTable = (HashTable*)malloc(sizeof(HashTable));
if (hashTable == NULL) {
printf("内存分配失败\n");
return NULL;
}
hashTable->size = size;
hashTable->count = 0;
hashTable->table = (HashNode**)malloc(sizeof(HashNode*) * size);
if (hashTable->table == NULL) {
printf("内存分配失败\n");
free(hashTable);
return NULL;
}
for (int i = 0; i < size; i++) {
hashTable->table[i] = NULL;
}
printf("创建哈希表成功,大小为:%d\n", size);
return hashTable;
}
int hashFunction(HashTable* hashTable, int key) {
return abs(key) % hashTable->size;
}
int insertHash(HashTable* hashTable, int key, int value) {
if (hashTable == NULL) {
printf("哈希表为空,无法插入\n");
return 0;
}
int hashIndex = hashFunction(hashTable, key);
HashNode* curNode = hashTable->table[hashIndex];
while (curNode != NULL) {
if (curNode->key == key) {
curNode->value = value;
printf("更新键:%d\n", key);
return 2;
}
curNode = curNode->next;
}
HashNode* newNode = (HashNode*)malloc(sizeof(HashNode));
if (newNode == NULL) {
printf("内存分配失败\n");
return 0;
}
newNode->key = key;
newNode->value = value;
newNode->next = hashTable->table[hashIndex];
hashTable->table[hashIndex] = newNode;
hashTable->count++;
printf("插入成功:键=%d, 值=%d\n", key, value);
return 1;
}
int searchHash(HashTable* hashTable, int key, int* value) {
if (hashTable == NULL || value == NULL) {
return 0;
}
int hashIndex = hashFunction(hashTable, key);
HashNode* curNode = hashTable->table[hashIndex];
while (curNode != NULL) {
if (curNode->key == key) {
*value = curNode->value;
return 1;
}
curNode = curNode->next;
}
return 0;
}
int DeleteHash(HashTable* hashTable, int key) {
if (hashTable == NULL) {
printf("哈希表为空,无法删除\n");
return 0;
}
int hashIndex = hashFunction(hashTable, key);
HashNode* curNode = hashTable->table[hashIndex];
HashNode* prevNode = NULL;
while (curNode != NULL) {
if (curNode->key == key) {
if (prevNode == NULL) {
hashTable->table[hashIndex] = curNode->next;
} else {
prevNode->next = curNode->next;
}
free(curNode);
hashTable->count--;
printf("删除键 %d 成功\n", key);
return 1;
}
prevNode = curNode;
curNode = curNode->next;
}
printf("删除失败,未找到键:%d\n", key);
return 0;
}
void printHashTable(HashTable* hashTable) {
if (hashTable == NULL) {
printf("哈希表为空\n");
return;
}
printf("\n=== 哈希表状态 ===\n");
printf("大小:%d, 元素个数:%d\n", hashTable->size, hashTable->count);
for (int i = 0; i < hashTable->size; i++) {
if (hashTable->table[i] != NULL) {
printf("桶 [%d]: ", i);
HashNode* curNode = hashTable->table[i];
while (curNode != NULL) {
printf("(%d:%d)", curNode->key, curNode->value);
if (curNode->next != NULL) {
printf(" -> ");
}
curNode = curNode->next;
}
printf("\n");
}
}
printf("=================\n");
}
void freeHash(HashTable* hashTable) {
if (hashTable == NULL) {
return;
}
for (int i = 0; i < hashTable->size; i++) {
HashNode* curNode = hashTable->table[i];
while (curNode != NULL) {
HashNode* temp = curNode;
curNode = curNode->next;
free(temp);
}
}
free(hashTable->table);
free(hashTable);
printf("哈希表已释放\n");
}
int main() {
printf("===== 哈希表测试程序 =====\n");
HashTable* ht = createHashTable(5);
if (ht == NULL) {
return 1;
}
printf("\n1. 测试插入操作:\n");
insertHash(ht, 1, 100);
insertHash(ht, 6, 200);
insertHash(ht, 11, 300);
insertHash(ht, 2, 400);
insertHash(ht, 3, 500);
printHashTable(ht);
printf("\n2. 测试更新操作:\n");
insertHash(ht, 1, 150);
printHashTable(ht);
printf("\n3. 测试查找操作:\n");
int value;
if (searchHash(ht, 6, &value)) {
printf("找到键 6, 值为:%d\n", value);
} else {
printf("未找到键 6\n");
}
if (searchHash(ht, 99, &value)) {
printf("找到键 99, 值为:%d\n", value);
} else {
printf("未找到键 99\n");
}
printf("\n4. 测试删除操作:\n");
DeleteHash(ht, 6);
DeleteHash(ht, 99);
printHashTable(ht);
printf("\n5. 测试负键值:\n");
insertHash(ht, -4, 600);
insertHash(ht, -9, 700);
printHashTable(ht);
freeHash(ht);
return 0;
}
===== 哈希表测试程序 ===== 创建哈希表成功,大小为:5 1. 测试插入操作:插入成功:键=1, 值=100 插入成功:键=6, 值=200 插入成功:键=11, 值=300 插入成功:键=2, 值=400 插入成功:键=3, 值=500 === 哈希表状态 === 大小:5, 元素个数:5 桶 [1]: (11:300) -> (6:200) -> (1:100) 桶 [2]: (2:400) 桶 [3]: (3:500) ================= 2. 测试更新操作:更新键:1 === 哈希表状态 === 大小:5, 元素个数:5 桶 [1]: (11:300) -> (6:200) -> (1:150) 桶 [2]: (2:400) 桶 [3]: (3:500) ================= 3. 测试查找操作:找到键 6, 值为:200 未找到键 99 4. 测试删除操作:删除键 6 成功 删除失败,未找到键:99 === 哈希表状态 === 大小:5, 元素个数:4 桶 [1]: (11:300) -> (1:150) 桶 [2]: (2:400) 桶 [3]: (3:500) ================= 5. 测试负键值:插入成功:键=-4, 值=600 插入成功:键=-9, 值=700 === 哈希表状态 === 大小:5, 元素个数:6 桶 [1]: (11:300) -> (1:150) 桶 [2]: (2:400) 桶 [3]: (3:500) 桶 [4]: (-9:700) -> (-4:600) ================= 哈希表已释放