繁体   English   中英

我不明白插入 function 机制将如何有人得到它(应该使用链表数组作为链接)?

[英]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)?

我创建了一个插入 function,它在链表数组中添加了学生 ID、名字、姓氏和 email。 我应该创建另一个 function 来检查某个学生是否可用并更新他的 email,但我不知道从哪里开始

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);
};

文件

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++;
};
  1. 您应该将firstNamelastNameemail作为三个单独的string字段存储在HashNode中,而不是在insert中连接它们。 这将为您省去很多麻烦。

  2. update function 中,您需要扫描学生已(或可能)添加到的链表; 也就是说,从table[hash(ID)]开始(顺便说一句, table[index]*(table + index)相同,至少对于指针[数组通常在 C 和 C++ 中被视为指针])。 您需要找到具有您要查找的密钥的节点(如果有),并更新其 email。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM