我不明白插入功能机制将如何有人得到它(应该使用链表数组作为链接)?
I dont understand how the insert function mechanism will be does someone get it(Having an array of linked lists as chaining should be used)?
我创建了一个插入函数,用于在链表数组中添加学生 ID、名字、姓氏和电子邮件。我应该创建另一个功能来检查某个学生是否有空并更新他的电子邮件,但我不知道从哪里开始使用该功能
头文件
#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 文件
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++;
};
您应该将 firstName
、lastName
和 email
作为三个单独的 string
字段存储在 HashNode
中,而不是将它们串联起来insert
。这将为您省去许多麻烦。
在update
函数中,您需要扫描学生被(或可能被)添加到的链表;也就是说,从 table[hash(ID)]
开始(顺便说一句,table[index]
与 *(table + index)
相同,至少对于指针而言 [数组通常被视为 C 和 C++ 中的指针])。您需要找到具有您查找的密钥的节点(如果有),并更新其电子邮件。
我创建了一个插入函数,用于在链表数组中添加学生 ID、名字、姓氏和电子邮件。我应该创建另一个功能来检查某个学生是否有空并更新他的电子邮件,但我不知道从哪里开始使用该功能
头文件
#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 文件
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++;
};
您应该将
firstName
、lastName
和email
作为三个单独的string
字段存储在HashNode
中,而不是将它们串联起来insert
。这将为您省去许多麻烦。在
update
函数中,您需要扫描学生被(或可能被)添加到的链表;也就是说,从table[hash(ID)]
开始(顺便说一句,table[index]
与*(table + index)
相同,至少对于指针而言 [数组通常被视为 C 和 C++ 中的指针])。您需要找到具有您查找的密钥的节点(如果有),并更新其电子邮件。