c - 打印哈希表

标签 c

我有一个关于一些哈希表操作的作业。我应该包括的操作是插入、查找、删除和打印完整的表内容。插入、删除和查找似乎工作得很好,但我不明白为什么我的打印功能不起作用。我真的什么也没打印出来。 (解决这个问题后我会打印在文件中。) 以下是函数(myfunctions.c,由标题 myfunctions.h 链接):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define B 26
#include "myfunctions.h"

int hashfunction(char *name){
int sum; unsigned long len;
sum = 0;
len = strlen(name);
for (int i=0; i<len; i++) {
    sum+=name[i];
}
return (sum%26);
}

void fillNode(NodeT *p, char *value){
p->name=value;
}

void insert(NodeT **Bucket, char *name){

NodeT *p=(NodeT*)malloc(sizeof(NodeT));

if(p){
    fillNode(p, name);
    int h=hashfunction(p->name);

    if(Bucket[h]==NULL){
        Bucket[h]=p;
        p->next=NULL;
    }
    else {
        p->next=Bucket[h];
        Bucket[h]=p;
    }
}
}

NodeT *findNode(NodeT **buckets, char *str){
int ok=0;
int i=0;
NodeT *aux=(NodeT*)malloc(sizeof(NodeT));

for(i=0;i<B;i++){
    NodeT *p=buckets[i];
    while(p){
        if(strcmp(p->name, str)==0)
        {
            ok=1;
            aux=p;
        }
        p=p->next;
    }
}
if(aux!=NULL) return aux;
else return NULL;
}

void deleteNode(NodeT **buckets, char *str){

NodeT **link=&buckets[hashfunction(str)];

while(*link){
    NodeT *aux=*link;
    if(strcmp(aux->name, str)==0){
        *link=aux->next;
        free(aux);
        break;
    }
    else link=&(*link)->next;
}
}

void printNodes(NodeT **buckets){

int i=0;
for(i=0;i<B;i++){
    NodeT *p=buckets[23];
    while(p){
        printf("%s\n", p->name);
        p=p->next;
    }

}
}

这是 main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myfunctions.h"
#define B 26

int main() {

FILE *input=fopen("/Users/andreibrasoveanu/Desktop/Teme CP/lab 6 - hashtables/lab 6 - hashtables/input.txt", "r");
FILE *output=fopen("/Users/andreibrasoveanu/Desktop/Teme CP/lab 6 - hashtables/lab 6 - hashtables/output.txt", "w");

char cmd[100];
char s[100];

NodeT **Bucket=(NodeT**)malloc(B*sizeof(NodeT*));
for(int i=0; i<26; i++) {
    Bucket[i]=NULL;
}

int hashcode;
char c;

while(fscanf(input, "%s\n", cmd)!=-1){
    c=cmd[0];

    strcpy(s, cmd+1);

    switch (c) {
        case 'i':
        {
            hashcode=hashfunction(s);
            insert(Bucket, s);
        }
            break;

        case 'd':{
            deleteNode(Bucket, s);
        }
            break;

        case 'f':{
            if(findNode(Bucket, s)!=NULL) printf("%s was found", s);
        }
            break;
        case 'l':{
            printNodes(Bucket);
        }
            break;
        default:
            break;
    }
}



return 0;
}

还有头文件(myfunctions.h):

typedef struct node{
char *name;
struct node *next;
}NodeT;

int hashfunction(char *name);
void insert(NodeT **Bucket, char *name);
NodeT *findNode(NodeT **buckets, char *str);
void deleteNode(NodeT **buckets, char *str);
void printNodes(NodeT **buckets);

输入文件为:

iBob
iMary
dBob
l

顺便说一句,我在 Xcode 中工作,这就是文件路径如此长的原因。

最佳答案

这些代码行不正确:

 void fillNode(NodeT *p, char *value){
    p->name=value;
 }   

结构元素“name”是一个指向 char 的指针,但我没有看到为此分配任何内存。所以它需要类似的东西:

     p->name = strdup(value);

此外,您不需要在每个开关标签上使用大括号:

    case 'i':
    {
        hashcode=hashfunction(s);
        insert(Bucket, s);
    }
        break;

可以

    case 'i':        
        hashcode=hashfunction(s);
        insert(Bucket, s);

        break;

关于c - 打印哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43332368/

相关文章:

使用较新的编译器编译 linux 2.6 内核模块

c - 混合 16 位线性 PCM 流并避免剪辑/溢出

c - 以 printf 作为参数的 for 循环

c++ - 如何在不在命令行 g++ 中运行 .so 文件的情况下使用它

c - 获取监视器编号小部件在 gtk 2 中已启用

c - 设置正确的文件路径

java - 在文件读取/打开时提供来自准备运行的程序的输出

c++ - GTK3 :How to receive input value from entry/input box?

c - C上一个信封的EVP非对称加解密

c - 如何将缓冲区放入 CompletionROUTINE 作为 WSARecvFrom 调用的一部分?