簡體   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