I have created a very simple Hash Table which stores names on an array but I want to store different type of data for example name,age,ID.I know I have to store the object of the person in the array but I cannot understand how to store the object and how do i display it.What changes do I need to make? Also if I need to search a name from it do I search for the name or the object in the hash table?
This is my hash table class
#pragma once
#include "iostream"
#include "string"
#include "Bucket.h"
using namespace std;
class HashTable
{
int size = 27;
string *arr = new string[size];
Since I want to store the object I guess this is how I would declare it Bucket *student=NULL; student=new Bucket;
public:
HashTable();
~HashTable();
int hashFunc(int index);
int sumASCII(string name);
void display();
void insert(string);
};
#include "HashTable.h"
HashTable::HashTable()
{
for (int i = 0; i < size; i++)
arr[i] = "0";
}
HashTable::~HashTable()
{
}
int HashTable::sumASCII(string key)
{
int sum = 0;
for (int i = 0; i < key.size(); i++)
{
sum = sum + key[i] % size;
}
return sum;
}
int HashTable::hashFunc(int index)
{
return (index + 1 * 2) % size;
}
void HashTable::insert(string name)
{
int index = sumASCII(name);
int check = 0;
while (check != 1)
{
if (arr[index] == "0")
{
I think this is where the object will be stored but instead of name it will be arr[index]=student
arr[index] = name;
check = 1;
}
else
{
index = hashFunc(index);
}
}
}
void HashTable::display()
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << endl;
}
}
and this is my class whos object i want to store
#pragma once
#include "iostream"
#include "string"
using namespace std;
class Bucket
{
public:
string name;
string school;
int age;
int ID;
Bucket();
~Bucket();
};
Aucun commentaire:
Enregistrer un commentaire