0

I am trying to build a Hash table and with what I have learned using online tutorials I have come up with the following code

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
const int SIZE = 100;

int hash(string);

class Node
{
   public:
      Node();
      Node(string);
      int hash(int value);
   private:
      string data;
      Node* next;

   friend class HashTable;
};

Node::Node() {
  data = "";
  next = NULL;
}

Node::Node(string) {
  data = "";
  next = NULL;
}

int Node::hash(int value) {
   int y;
   y = value % SIZE;
}

class HashTable {
public:
    HashTable();
      HashTable(int);
      ~HashTable();
      void insertItem(string);
      bool retrieveItem(string);

private:
      Node* ht;
};

HashTable::HashTable() {
  ht = new Node[SIZE];
}

HashTable::HashTable(int max) {
  ht = new Node[max];
}

HashTable::~HashTable() {
   delete[] ht;
}

void HashTable::insertItem(string name) {
    int val = hash(name);
    for (int i = 0; i < name.length(); i++)
        val += name[i];
}

bool HashTable::retrieveItem(string name) {
    int val = hash(name);
    if (val == 0 ) {
        cout << val << " Not Found " << endl;
    }
    else {
        cout << val << "\t" << ht->data << endl;
    }
}

void print () {
    //Print Hash Table with all Values
}

int main() {

   HashTable ht;
   ht.insertItem("Allen");
   ht.insertItem("Tom");

   ht.retrieveItem("Allen");

   //data.hash(int val);
   //cout << ht;

   system("pause");
   return 0;
}


int hash(string val) {
    int key;
    key = val % SIZE;
}

I am trying to insert string values and validate if the name exists using the retrieveItem function. Also, how do I go about printing the HashTable with values.

Any help will be highly appreciated.

Vish

Vish
  • 383
  • 2
  • 8
  • 25
  • That my code does not work! What part of the code is wrong? Or if the entire code is wrong, what is the solution? – Vish May 13 '12 at 17:04
  • 2
    There are a lot of things wrong; how do you know how big the array is if you have two constructors and don't save the length? Why are you trying to use modulus on a string? You're not actually inserting anything in `insertItem` and not returning anything in `retrieveItem`. – Seth Carnegie May 13 '12 at 17:07
  • i want to use modulo to get the ascii value of the string and use ascii total % size = index position in table and store the string there. e.g. for string "Allen" total ascii value is 476 % 101 (table size) gives me 76 as the position in the index table to store the data. – Vish May 13 '12 at 17:13
  • Steer towards a method signature like `const std::string&` in preference to `string`. [`using namespace std` is something you'll want to avoid](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), it can cause a lot of confusion and conflict. Passing by `const` reference avoids duplicating the argument and accidental mutations. – tadman Dec 30 '18 at 21:21

2 Answers2

1

There's quite a bit wrong here. Just glancing at a random spot in the middle of the code, I found:

Node::Node(string) {
  data = "";
  next = NULL;
}

This doesn't save the value of the string that was passed in. I'm guessing you really wanted something more like:

Node::Node(string value) : data(value), next(NULL) {}

The next thing I saw was:

int Node::hash(int value) {
   int y;
   y = value % SIZE;
}

This computes a value and puts it into y (a local variable) but doesn't return anything, so the value you've computed is immediately lost. If you attempt to use its return value, you get undefined behavior (actually, I don't remember for sure -- it seems like in C you get UB only if you try to use the return value, but in C++ it's UB to do this at all -- regardless of the technical details, though, it's clearly not what you want and needs to be fixed).

Like I said, I just glanced at one spot in the code and found though -- I doubt they're the only problems. The bottom line is that you probably need to back up a bit, figure out what each function is supposed to do, and go through them one by one and ensure each is doing what it's supposed to.

One other bit of advice: instead of dealing with dynamically allocating an array on your own, I'd used std::vector.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks Jerry! I will look into the functions. I am just starting with C++ so learning as i go... – Vish May 13 '12 at 17:30
0

just use stl hashmap or for threadsafe hashmap use intel thread building blocks library

Chuck Norrris
  • 284
  • 5
  • 12
  • Thanks Kay! But I am a beginner so don't know what you mean ;). I am trying to do this using linked list and hash chaining – Vish May 13 '12 at 19:50