首页 > 解决方案 > 如何在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);

  // ...
}

标签: cgetinserthashtable

解决方案


推荐阅读