【问题标题】:How to hash in c++如何在 C++ 中散列
【发布时间】:2014-11-09 06:02:20
【问题描述】:

我的程序应该从命令行接收一个文件,该文件包含一个名称列表(不超过十个字符),后跟一个空格,然后是年龄,所有这些都由新行分隔。我要创建一个大小为 10 的哈希表,使用单独的链接,哈希函数 h(x) = x mod 10,然后在完成后打印出该表。

代码:

#include <iostream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <ctype.h>
#include <stdio.h>

using namespace std;

struct node
{
    char name[10];
    int age;
    node *next;

    node()
    {
        memset(name, 0x0, sizeof(name));
        age = 0;
    }
};

int main(int argc, char *argv[])
{
    node *heads = new node[10];
    string currentLine;
    char c;
    int index = 0, fileAge, hashValue = 0;
    node *current;

    current = new node;


    ifstream input(argv[1]);

    if (input.is_open()) //while file is open
    {
        while (getline(input, currentLine)) //checks every line
        {
            istringstream iss(currentLine);
            while (iss >> c)
            {
                if (iss.eof())
                    break;

                if (isdigit(c))
                {
                    iss.putback(c);
                    iss >> fileAge;
                    hashValue = fileAge % 10;
                    current->age = fileAge;

                    if ((&heads[hashValue]) == NULL)
                        heads[hashValue] = *current;
                    else
                    {
                        current->next = &heads[hashValue];
                        heads[hashValue] = *current;
                    }
                }

                else
                {
                    current->name[index] = c;
                    index++;
                }
            }
        }
    }

    for (int x = 0; x < 10; x++)
    {
        printf(" Index %d: ", x);

        current = &heads[x];
        if (current != NULL)
        {
            if (!string(current->name).empty())
                printf("%s (%d), ", current->name, current->age);
        }

        printf("\b\b\b\n");
    }
}

输入文件:

Alice 77
John 68
Bob 57
Carlos 77

预期输出:

.
.
.
Index 6: 
Index 7: Alice (77), Bob (57), Carlos (77)
Index 8: John (68)
.
.
.

实际输出:

Index 7: AliceJohnBobM (77)
Index 8: Alice John (68),

我不明白是什么导致了这个问题,我们将不胜感激。

【问题讨论】:

    标签: c++ hash chaining


    【解决方案1】:

    问题是,这是无限循环。

    current = &heads[x];
    while (current != NULL)
    {
        printf("%s (%d), ", current->name, current->age);
    }
    

    下面的打印值正确...

    current = &heads[7];
    printf("%s (%d), ", current->name, current->age);
    

    这样的事情可以处理多行,但我的代码中有一个小问题。我相信你应该能够解决这个问题。您需要正确设置'next'项并正确遍历链表。

    struct node
    {
        char name[10];
        int age;
        node *next;
    
        node()
        {
            memset(name, 0x0, sizeof(name));
            age = 0;
            next = NULL;
        }
    };
    
    int main(int argc, char *argv[])
    {
        node *heads = new node[10];
    
        string currentLine;
        char c;
        int index = 0, fileAge, hashValue = 0;
        node *current;
    
        ifstream input(argv[1]);
    
        if (input.is_open()) //while file is open
        {
            while (getline(input, currentLine)) //checks every line
            {
                current = new node();
    
                istringstream iss(currentLine);
                while (iss >> c)
                {
                    if (iss.eof())
                        break;
    
                    if (isdigit(c))
                    {
                        current->name[index] = 0;
    
                        iss.putback(c);
                        iss >> fileAge;
                        hashValue = fileAge % 10;
                        current->age = fileAge;
                    }
                    else
                    {
                        current->name[index] = c;
                        index++;
                    }
                }
    
                if ((&heads[hashValue]) == NULL)
                    heads[hashValue] = *current;
                else
                {
                    if ((&heads[hashValue])->next == NULL)
                        heads[hashValue] = *current;
    
                    heads[hashValue].next = current;
    
                    node* next = new node;
                    heads[hashValue].next->next = next;
                    current = next;
                    index = 0;
                }
            }
        }
    
        for (int x = 0; x < 10; x++)
        {
            printf(" Index %d: ", x);
    
            node *currentNode = &heads[x];
            while (currentNode != NULL && !string(currentNode->name).empty())
            {
                printf("%s (%d), ", currentNode->name, currentNode->age);
                currentNode = currentNode->next;
            }
    
            printf("\b\b\n");
        }
    }
    

    【讨论】:

    • 另外,您没有初始化姓名、年龄。当前=新节点;
    • 非常感谢。如果有多个输入,您还可以帮我解决这个问题吗?我将使用超过一行的输入文件更新原始帖子,并在使用此新代码编译时显示输出。
    • 查看我的最新答案。
    猜你喜欢
    • 2012-11-28
    • 2018-12-30
    • 1970-01-01
    • 2014-08-02
    • 2021-01-20
    • 2015-07-17
    • 2018-10-27
    • 2022-01-09
    • 2012-08-22
    相关资源
    最近更新 更多