使用单链表插入哈希 Table

Insertion in Hash Table using Singly Linked Lists

我是新来的,我需要一些帮助来解决这个问题,问题是它存储第一个条目的值,但是当我创建一个列表时,例如当我想插入 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;
            }
        }
    }

当您分配 temp = NewNode; 时,您实际上并没有将前一个 temp->next 的值设置为 NewNode。不需要设置tempNewNode,需要设置一次temp->nextNewNodetemp->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