在哈希表中插入使用单一链接列表的方法。

问题描述 投票:-1回答:1

我是新来的,我需要一些帮助,问题是它存储了第一个条目的值,但是当我在创建一个列表时,例如当我想插入 22在我插入 2 前的节点,它的行为就像它在 2,但实际上它并没有创建,我不知道为什么。需要帮助这个问题,谢谢。

 void insertKey(int key) {

        int i = Hash(key);
        Node* temp = HashTable[i];

        Node* NewNode = new Node;
        NewNode->key = key;
        NewNode->next = NULL;
        if (temp == NULL) {
            HashTable[i] = NewNode;
        }
        else
        {
            while (temp != NULL) {
                cout << "NOTHere ";
                temp = temp->next;
            }
            if (temp == NULL) {
                cout << "FoundYa ";
                temp = NewNode;
            }
        }
    }
c++ hashtable singly-linked-list
1个回答
1
投票

当你将 temp = NewNode; 你实际上并没有设置之前的数值。temp->nextNewNode. 你不需要设置 tempNewNode,你需要设置 temp->nextNewNode 曾经 temp->next == NULL.

你目前正在做的是:

[ node 1 ] --> nullptr
                 |
                 |
        assign nullptr to temp
                 |
                 V
                temp <--- then assign NewNode to temp

你需要做的是:

[ node 1 ] --> nullptr
    |
    |
  assign this to temp
    |
    V
   temp --> nullptr <-- assign NewNode to this
© www.soinside.com 2019 - 2024. All rights reserved.