打印哈希表函数(它不起作用,并且程序崩溃) [英] Print a hash table function (it does not work and the program it crashes)

查看:67
本文介绍了打印哈希表函数(它不起作用,并且程序崩溃)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习哈希表,我必须编写一个显示并列出哈希表的程序。键是txt文件(词典)中的单词列表。

I just started to learn about hash tables.I have to write a program which displays and list a hash table.The keys are a list of words from a txt file which is a dictionary.

我写了代码,但是当我调用list函数打印表时,在主文件中有一个断点。程序崩溃了。如何解决?list函数在函数文件中。

I wrote the code but I got a breakpoint in main file when I call the list function to print the table.The program crashes.How to fix it ?The list function is in functions file.

这是我写的代码:

主文件

#include <iostream>
#include <fstream>
#include <string>
#include "header.h"
using namespace std;

int main()
{
    elem* HT[M];
    initHT(HT);
    ifstream fs("dictionar_termeni_PC.txt");

    if (fs)
    {
        std::string text;

        while (!fs.eof())
        {
            fs >> text;
            char* S = new char[text.length() + 1];
            strcpy_s(S, text.length() + 1, text.c_str());
            insert(HT, S);
        }
        list(HT);
    }
}

功能文件

#include <iostream>
#include <string>
#include "header.h"
using namespace std;

int f(char* key)
{
    int i, suma;
    suma = 0;

    for (i = 0; i < strlen(key); i++)
    {
        suma = suma + *(key + i);
    }

    return suma%M;
}

void initHT(elem *HT[])
{
    for (int i = 0; i < M; i++)
    {
        HT[i] = nullptr;
    }
}

elem* find(elem *HT[], char* key)
{
    int h;
    elem* p;
    h = f(key);
    p = HT[h];

    while (p != 0)
    {
        if (strcmp(key, p->key) == 0)
            return p;
        p = p->leg;
    }

    return 0;
}

void insert(elem *HT[M], char* key)
{
    elem *p = new elem;
    p->key = new char[strlen(key) + 1];
    strcpy_s(p->key, strlen(p->key) + 1, key);
    int h = f(key);

    if (HT[h] == nullptr)
    {
        HT[h] = p;
        p->leg = nullptr;
    }
    else
    {
        elem* q = find(HT, key);

        if (q == nullptr)
        {
            p->leg = HT[h];
            HT[h] = p;
        }
    }
}

void list(elem* HT[])
{
    for (int i = 0; i<M; i++) {
        if (HT[i] != 0) {
            cout << "Hash code is " << i << endl;
            elem* p = HT[i];

            while (p != 0) {
                cout << p->key;
                p = p->leg;
            }
        }
    }
}

标题文件

#ifndef HEADER_H_
#define HEADER_H_
#define M 10000
struct elem {
    char* key;
    elem* leg;
};


int f(char* key);
void initHT(elem *HT[]);
elem* find(elem *HT[], char* key);
void insert(elem *HT[M], char* key);
void list(elem* HT[]);
#endif


推荐答案

我认为可能是与您声明的 M 常量相关,它假定每次执行程序时文件中都会有10.000行。

I think it perhaps comes related to the M constant you have declared, it assumes every time your program is executed that there will be 10.000 lines in your file.

void list(elem* HT[])
{
    elem* tmp;/*call it tmp or temp*/
    for (int i = 0; i < M/*use a parameter instead*/; i++) {
        if (HT[i] != 0 /*Use NULL instead*/) {
            cout << "\nHash code is " << i << ", key: ";
            tmp = HT[i];
            while (tmp != 0) {
                cout << tmp->key;
                tmp = tmp->leg;
            }
        }
    }
}

什么我会做的是:

#include <cstring>

struct elem {
    std::string key;
    std::string value;//because a hashtable has key-value pairs
    elem* next;
};

std :: string 自带函数。

如果只是为了探索语言,则可以改用 std :: map 进行修改满足您的需求。

If it is just to explore the language, you could use instead std::map and adapt it to your needs. Hope it helped.

由于Java的实现,我建议 map ,记住数据结构的设计取决于您,如果需要hastable沿值使用映射分配键,如果不需要,请使用set。

I suggest map due to Java implementation, remember the design of a data structure is up to you, if you need the hastable to allocate the key along the value use map, if not, use set.

M。 Goodrich T.,R。Tamassia和M. Goldwasser H。,《 Java中的数据结构和算法》。约翰·威利Sons,2014年。他们实现了键值实现。

M. Goodrich T., R. Tamassia, and M. Goldwasser H., Data Structures and Algorithms in Java. John Wiley & Sons, 2014. They make a key-value implementations.

这篇关于打印哈希表函数(它不起作用,并且程序崩溃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆