Search code examples
c++hashchaining

How to hash in c++


My program is supposed to take in a file from the command line, which is to contain a list of names (no longer than ten characters) followed by a space and then age, all separated by new lines. I am to create a hash table of size 10, using separate chaining, with the hash function h(x) = x mod 10, and then print out the table when complete.

Code:

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

Input file:

Alice 77
John 68
Bob 57
Carlos 77

Expected Output:

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

Actual output:

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

I do not understand what is causing this problem and any and all help will be appreciated.


Solution

  • The problem is, this is infinite loop.

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

    below prints value correctly...

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

    Something like this will work with multiple lines, but there is small issue in my code. I am sure you should be able to fix that. You need to set the 'next' item correctly and traverse linked list correctly.

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