2016-09-21 35 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中的內容,將更改已存儲的第一個鍵的值,並在地圖中再次存儲相同的指針。所以這兩個鍵將具有相同的值。

這也會導致映射中的指針指向無效內存區域(如果它們被刪除或調用函數返回,並將傳入的數組超出範圍)。

當您將密鑰存儲在哈希映射中時,您需要複製密鑰。