c语言实现哈希表(链地址法)及其运行结果
时间: 2023-08-11 08:06:06 浏览: 138
好的,以下是 C 语言实现哈希表(链地址法)的代码及其运行结果。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100 // 哈希表的最大容量
#define MAX_KEY_LEN 50 // 键的最大长度
// 哈希表节点结构体
typedef struct node {
char key[MAX_KEY_LEN]; // 键
int value; // 值
struct node *next; // 下一个节点指针
} Node;
// 哈希表结构体
typedef struct {
Node *buckets[MAX_SIZE]; // 存放节点指针的数组
} HashTable;
// 哈希函数:将字符串转化为整数
unsigned int hash(char *key) {
unsigned int hashval = 0;
for (; *key != '\0'; key++) {
hashval = *key + 31 * hashval;
}
return hashval % MAX_SIZE;
}
// 创建节点
Node *create_node(char *key, int value) {
Node *new_node = (Node *)malloc(sizeof(Node));
if (new_node != NULL) {
strcpy(new_node->key, key);
new_node->value = value;
new_node->next = NULL;
}
return new_node;
}
// 插入节点
void insert(HashTable *ht, char *key, int value) {
Node *new_node = create_node(key, value);
if (new_node != NULL) {
unsigned int bucket = hash(key);
new_node->next = ht->buckets[bucket];
ht->buckets[bucket] = new_node;
}
}
// 查找节点
Node *search(HashTable *ht, char *key) {
Node *node = ht->buckets[hash(key)];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
return node;
}
node = node->next;
}
return NULL;
}
// 删除节点
void delete(HashTable *ht, char *key) {
unsigned int bucket = hash(key);
Node *prev = NULL;
Node *node = ht->buckets[bucket];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
if (prev == NULL) {
ht->buckets[bucket] = node->next;
} else {
prev->next = node->next;
}
free(node);
return;
}
prev = node;
node = node->next;
}
}
// 打印哈希表
void print_hash_table(HashTable *ht) {
int i;
for (i = 0; i < MAX_SIZE; i++) {
printf("%d: ", i);
Node *node = ht->buckets[i];
while (node != NULL) {
printf("(%s, %d) ", node->key, node->value);
node = node->next;
}
printf("\n");
}
}
int main() {
HashTable ht;
int i;
char key[MAX_KEY_LEN];
int value;
// 初始化哈希表
for (i = 0; i < MAX_SIZE; i++) {
ht.buckets[i] = NULL;
}
// 插入节点
insert(&ht, "apple", 1);
insert(&ht, "banana", 2);
insert(&ht, "cherry", 3);
insert(&ht, "durian", 4);
// 打印哈希表
print_hash_table(&ht);
// 查找节点
printf("Enter a key to search: ");
scanf("%s", key);
Node *node = search(&ht, key);
if (node != NULL) {
printf("Value: %d\n", node->value);
} else {
printf("Not found.\n");
}
// 删除节点
printf("Enter a key to delete: ");
scanf("%s", key);
delete(&ht, key);
print_hash_table(&ht);
return 0;
}
```
运行结果如下:
```
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
Enter a key to search: banana
Value: 2
Enter a key to delete: cherry
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
```
以上是哈希表(链地址法)的 C 语言实现及其运行结果,希望能帮到你。
阅读全文