I have created an insert function that adds a students id,first name,last name and email in an array of linked list. I should create another function that checks if a certain student is available and update his email, but I don't know where to start with that function
HEADER FILE
#pragma once
#include <iostream>
#include <string>
using namespace std;
class HashTable
{
private:
struct HashNode
{
string key;
string value;
HashNode* next;
};
HashNode* table[100];
int currentSize;
int maxSize;
public:
HashTable(int x);
void insert(string ID, string firstName, string lastName, string email);
bool update(string ID, string newEmail);
};
CPP FILE
HashTable::HashTable(int x)
{
maxSize = x;
};
void HashTable::insert(string ID, string firstName, string lastName, string email)
{
HashNode x;
x.key = ID;
x.value = firstName + " " + lastName + " " + email;
int index = hash(x.key);
if (*(table + index) == NULL)
{
*(table + index) = new HashNode;
(*(table + index))->key = x.key;
(*(table + index))->value = x.value;
(*(table + index))->next = NULL;
}
else
{
HashNode* temp = *(table + index);
while (temp->next != NULL)
temp = temp->next;
HashNode* newNode = new HashNode;
newNode->key = x.key;
newNode->value = x.value;
newNode->next = NULL;
temp->next = newNode;
temp = NULL;
newNode = NULL;
}
currentSize++;
};
You should store firstName
, lastName
and email
as three separate string
fields in HashNode
instead of concatenating them in insert
. That will save you numerous headaches.
In update
function, you need to scan the linked list the student was (or could be) added to; that is, that starting at table[hash(ID)]
(btw, table[index]
is the same as *(table + index)
, at least for pointers [arrays are often treated as pointers in C and C++]). You need to find the node having the key you look for (if any), and update its email.