如何在c++中散列

How to hash in c++

本文关键字:c++      更新时间:2023-10-16

我的程序应该从命令行接收一个文件,其中包含一个名称列表(不超过十个字符),后跟一个空格,然后是年龄,所有这些都用新行分隔。我将使用单独的链接创建一个大小为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("bbbn");
    }
}

输入文件:

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),

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

问题是,这是一个无限循环。

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

下面正确打印值。。。

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

这样的东西可以在多行中使用,但我的代码中有一个小问题。我相信你应该能够解决这个问题。您需要正确设置"下一个"项目并正确遍历链表。

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("bbn");
    }
}
相关文章:
  • 没有找到相关文章