2016-09-21 60 views
1

我的哈希映射打印功能看起来有问题。这是打印正确的值,但不正确的键。在下面的代码中,我得到了两个键/值对的用户输入,它只打印出第二个输入的键。哈希映射打印功能不更新集合

例如,对于第一对,我将输入12 /“e”,第二对,我将输入15 /“f”,输出将为f | 12,f | 15.有人知道发生了什么吗?我认为它可能是char数组的东西,我会使用字符串,但我只能使用基元来完成这个任务。 char数组的大小为100,只是一个任意大的数字,我认为不会被用户密钥超过。谢谢!

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <iostream> 
using namespace std; 

typedef struct _node 
{ 
    char *key; 
    int value;   /* For this, value will be of type int */ 
    struct _node *next; /* pointer to the next node in the list */ 
} node; 

/* HashMap class */ 
class HashMap 
{ 
private: 
    node ** hashTable; 
    int numSlots; 
public: 
    /* Initializes a hashmap given its set size */ 
    HashMap(int size) 
    { 
     numSlots = size; 
     hashTable = new node*[size] ; 
     for (int i = 0; i < size; i++) 
     { 
      hashTable[i] = NULL; 
     } 
    } 

    /*** Hash function. ***/ 

    int hash(char *s) 
    { 
     int i; 
     int sum = 0; 

     for (i = 0; * (s + i) != '\0'; i++) 
     { 
      sum += *(s + i); 
     } 

     return (sum % numSlots); 
    } 

    /* Create a single node. */ 
    node *create_node(char *key, int value) 
    { 
     node *result = new node(); 
     result->key = key; 
     result->value = value; 
     result->next = NULL; 

     return result; 
    } 

    /* 
    *Stores given key/value pair in hashmap 
    *returns boolean for success/failure 
    */ 

    void set (char* key, int value) 
    { 
     int keyValue = hash(key); 
     node *current = hashTable[keyValue]; 
     node *original = current; 
     node *newNode; 
     if (current == NULL) 
     { 
      hashTable[keyValue] = create_node(key, value); 
     } 
     else 
     { 
      while (current != NULL) 
      { 
       current = current -> next; 
      } 

      if (current == NULL) 
      { 
       newNode = create_node(key, value); 
       newNode -> next = original; 
       hashTable[keyValue] = newNode; 
      } 
     } 
    } 

    /* Prints hash table */ 

    void print_hash_table() 
    { 
     int i; 
     node *listIterator = NULL; 

     for (i = 0 ; i < numSlots ; i++) 
     { 
      listIterator = hashTable[i]; 

      if (listIterator != NULL) 
      { 
       cout << listIterator->key << " | "; 
       while (listIterator != NULL) 
       { 
        cout << listIterator->value << " "; 
        listIterator = listIterator -> next; 
       } 
       cout << endl; 
      } 


     } 

    } 
}; 


int main() 
{ 
    HashMap hash (128); 
    char key[100]; 
    int value; 

    cout << "Enter element to be inserted: "; 
    cin >> value; 
    cout << "Enter key at which element to be inserted: "; 
    cin >> key; 
    hash.set(key, value); 
    cout << "Enter element to be inserted: "; 
    cin >> value; 
    cout << "Enter key at which element to be inserted: "; 
    cin >> key; 
    hash.set(key, value); 
    hash.print_hash_table(); 
    return 0; 
} 
+3

欢迎来到Stack Overflow!这听起来像你可能需要学习如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。详细阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

+0

该代码很难调试... .code显示奇怪的行为...检查http://code.geeksforgeeks.org/pPUDRe –

回答

0

您正将一个字符指针传递给set,并将该指针存储在您的地图中。然后你改变key中的内容,将更改已存储的第一个键的值,并在地图中再次存储相同的指针。所以这两个键将具有相同的值。

这也会导致映射中的指针指向无效内存区域(如果它们被删除或调用函数返回,并将传入的数组超出范围)。

当您将密钥存储在哈希映射中时,您需要复制密钥。