微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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 * 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 举报,一经查实,本站将立刻删除。