Print a hash table function (it does not work and the program it crashes)

一世执手 提交于 2020-04-18 00:53:17

问题


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.

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.

Here is code I wrote:

main 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);
    }
}

functions file

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

header file

#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

回答1:


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;
            }
        }
    }
}

What I would do is:

#include <cstring>

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

std::string comes with its own functions.

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

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, and M. Goldwasser H., Data Structures and Algorithms in Java. John Wiley & Sons, 2014. They make a key-value implementations.



来源:https://stackoverflow.com/questions/60954525/print-a-hash-table-function-it-does-not-work-and-the-program-it-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!