跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
C 语言字典实现
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
== 字典实现步骤 == 以下是C语言中字典的完整实现步骤。 === 1. 定义键值对结构 === 每个键值对包含一个键(key)、一个值(value)和一个指向下一个键值对的指针(用于链地址法)。 <syntaxhighlight lang="c"> typedef struct KeyValuePair { char *key; int value; struct KeyValuePair *next; } KeyValuePair; </syntaxhighlight> === 2. 定义字典结构 === 字典包含一个固定大小的数组(哈希表)和一个记录当前元素数量的变量。 <syntaxhighlight lang="c"> #define TABLE_SIZE 100 typedef struct Dictionary { KeyValuePair *table[TABLE_SIZE]; int count; } Dictionary; </syntaxhighlight> === 3. 哈希函数 === 哈希函数将字符串键转换为数组索引。这里使用简单的乘法哈希: <syntaxhighlight lang="c"> unsigned int hash(const char *key) { unsigned int hash_value = 0; for (int i = 0; key[i] != '\0'; i++) { hash_value = (hash_value * 31) + key[i]; } return hash_value % TABLE_SIZE; } </syntaxhighlight> === 4. 字典初始化 === 初始化字典时,将所有数组元素设为NULL。 <syntaxhighlight lang="c"> void dictionary_init(Dictionary *dict) { for (int i = 0; i < TABLE_SIZE; i++) { dict->table[i] = NULL; } dict->count = 0; } </syntaxhighlight> === 5. 插入键值对 === 插入时,先计算哈希值,然后检查是否已存在相同键。若存在则更新值,否则添加到链表头部。 <syntaxhighlight lang="c"> void dictionary_insert(Dictionary *dict, const char *key, int value) { unsigned int index = hash(key); KeyValuePair *current = dict->table[index]; // 检查键是否已存在 while (current != NULL) { if (strcmp(current->key, key) == 0) { current->value = value; // 更新值 return; } current = current->next; } // 创建新键值对 KeyValuePair *new_pair = malloc(sizeof(KeyValuePair)); new_pair->key = strdup(key); new_pair->value = value; new_pair->next = dict->table[index]; dict->table[index] = new_pair; dict->count++; } </syntaxhighlight> === 6. 查找键值对 === 查找时,先计算哈希值,然后遍历链表查找匹配的键。 <syntaxhighlight lang="c"> int dictionary_lookup(Dictionary *dict, const char *key, int *value) { unsigned int index = hash(key); KeyValuePair *current = dict->table[index]; while (current != NULL) { if (strcmp(current->key, key) == 0) { *value = current->value; return 1; // 找到 } current = current->next; } return 0; // 未找到 } </syntaxhighlight> === 7. 删除键值对 === 删除时,先找到键值对,然后从链表中移除并释放内存。 <syntaxhighlight lang="c"> int dictionary_delete(Dictionary *dict, const char *key) { unsigned int index = hash(key); KeyValuePair *current = dict->table[index]; KeyValuePair *prev = NULL; while (current != NULL) { if (strcmp(current->key, key) == 0) { if (prev == NULL) { dict->table[index] = current->next; } else { prev->next = current->next; } free(current->key); free(current); dict->count--; return 1; // 删除成功 } prev = current; current = current->next; } return 0; // 未找到键 } </syntaxhighlight> === 8. 清理字典 === 释放所有动态分配的内存。 <syntaxhighlight lang="c"> void dictionary_free(Dictionary *dict) { for (int i = 0; i < TABLE_SIZE; i++) { KeyValuePair *current = dict->table[i]; while (current != NULL) { KeyValuePair *temp = current; current = current->next; free(temp->key); free(temp); } } dict->count = 0; } </syntaxhighlight>
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)