c - 如何在C中插入和获取哈希表的密钥?
问题描述
我完全不知道如何用 C 编程语言实现哈希表的插入和获取函数。我有一个头文件、哈希函数文件和一个用于不同函数的文件。
哈希函数:
#include "hashfunc.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
const int HASHVEKSIZE = 1048576;
uint32_t tilpro_hash(const char * s) {
uint32_t hash = 0;
//...
}
void put(Nod ** hashtable, char * key, char * value) {
// TODO
}
char * get(Nod ** hashtable, char * key) {
// TODO
}
void init(Nod ** vek) {
// TODO
}
头文件:
#ifndef tproHASHFUNC_H
#define tproHASHFUNC_H
#include <stdint.h>
#include "lista.h" // en headerfil för en modifierad dubbellänkad lista p3
uint32_t tilpro_hash(const char * s) ;
void put(Nod ** hashtable, char * key, char * value);
char * get(Nod ** hashtable, char * key);
void init(Nod ** vek);
#endif
还有一个结构节点的文件:
#ifndef LISTA_H
#define LISTA_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct nod {
char key[512];
char value[512];
struct nod * next;
struct nod * prev;
};
typedef struct nod Nod;
void insertnod(Nod ** list, Nod * tobeadded);
void removenod(Nod ** list, Nod * toberemoved);
void printnod(Nod * node);
void printlist(Nod * node);
Nod * search(Nod * node, char * key);
#endif /* LISTA_H */
我发现很难知道如何开始编写 void put() 和 char * get()。很难知道所有的指针和东西。请帮忙!
main.c 看起来像:
#include "hashfunc.h"
// ...
extern const int HASHVEKSIZE;
// ...
int main() {
Nod ** myhashvek = malloc(sizeof(Nod *) * HASHVEKSIZE);
init(myhashvek);
put(myhashvek, "Adam", "123321");
char * s = get(myhashvek, "Adam");
printf("Adam -> value = %s expecting Adam\n", s);
// ...
}
解决方案
推荐阅读
- android - 应用程序在不同设备上扫描信标时的不同反应
- python-3.x - python无法从类中的方法打印列表
- css - 是否可以为 onsen ui 2 添加 RTL 方向?
- android-emulator - 连续错误:PANIC:损坏的 AVD 系统路径。检查您的 ANDROID_SDK_ROOT 值 [C:\Users\NoteBook\Downloads\Compressed\sdktools]
- c# - 如何使用 SharePoint 列表选择值填充 WinForms 组合框?
- python - Pandas Melt:从列到行
- javascript - 访问对象 JavaScript
- here-api - 有没有办法更改此处地图中特定项目的 zIndex?
- c# - CsvHelper - 使用映射写入 SQL 数据库
- c# - 为什么使用 Office365 SMTP 进行身份验证声称正确的凭据无效?