[英]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)?
I have created an insert function that adds a students id,first name,last name and email in an array of linked list.我创建了一个插入 function,它在链表数组中添加了学生 ID、名字、姓氏和 email。 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
我应该创建另一个 function 来检查某个学生是否可用并更新他的 email,但我不知道从哪里开始
HEADER FILE HEADER 文件
#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
.您应该将
firstName
、 lastName
和email
作为三个单独的string
字段存储在HashNode
中,而不是在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;在
update
function 中,您需要扫描学生已(或可能)添加到的链表; 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++]).也就是说,从
table[hash(ID)]
开始(顺便说一句, table[index]
与*(table + index)
相同,至少对于指针[数组通常在 C 和 C++ 中被视为指针])。 You need to find the node having the key you look for (if any), and update its email.您需要找到具有您要查找的密钥的节点(如果有),并更新其 email。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.