如何在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 * value); 
char * get(Nod ** hashtable,char * key); 

void init(Nod ** vek);

#endif

还有一个用于 struct Node 的文件

#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);

  // ...
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)