Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 键值不会打印,尽管它被用于对数据进行排序,但查找原因是什么?_C_Hashmap_Printf - Fatal编程技术网

C 键值不会打印,尽管它被用于对数据进行排序,但查找原因是什么?

C 键值不会打印,尽管它被用于对数据进行排序,但查找原因是什么?,c,hashmap,printf,C,Hashmap,Printf,我目前是一名学习数据结构的学生,尤其是hashmap。我一直试图在程序结束时通过主函数中的printf命令打印结果。尽管我在上一个while循环中使用了这些键,但我无法在最后一部分中打印这些键 我尝试通过使用打印来检查insertMap函数(如下所示),以指示密钥是否已被实际存储,并且已被存储,这样就不会出现错误 int main (int argc, const char * argv[]) { /*Write this function*/ const char *

我目前是一名学习数据结构的学生,尤其是hashmap。我一直试图在程序结束时通过主函数中的printf命令打印结果。尽管我在上一个while循环中使用了这些键,但我无法在最后一部分中打印这些键

我尝试通过使用打印来检查insertMap函数(如下所示),以指示密钥是否已被实际存储,并且已被存储,这样就不会出现错误

     int main (int argc, const char * argv[]) {
    /*Write this function*/
    const char * filename = argv[1];
    struct hashMap ht;
    char * entWord;
    int * oldAt;
    int repAt;
    int i = 0;

    FILE * fp = fopen(filename, "r");

    initMap(&ht, 100);
    while(!feof(fp)){
          entWord = getWord(fp);
        /*  printf("%s \n", entWord);  */
          if(!containsKey(&ht,entWord))
            insertMap(&ht, entWord, 1);
          else{
            oldAt = atMap(&ht, entWord);
            repAt = *oldAt;
            removeKey(&ht, entWord);
            insertMap(&ht, entWord, repAt+1);
          }
          free(entWord);
    }

    for(i = 0; i < ht.tableSize; i++){
      if(ht.table[i] != NULL){
        printf("%s: %d \n",ht.table[i]->key,ht.table[i]->value);
      }
    }

    fclose(fp);
      return 0;
}

示例输出::1:2:5:2示例输出::1:2:5:2
 void insertMap (struct hashMap * ht, KeyType k, ValueType v)
 {  /*write this*/
int idx = stringHash1(k) % ht->tableSize;
struct hashLink * hlnk;
struct hashLink * plink;
assert(ht);

if(ht->table[idx] == NULL){
    hlnk = (struct hashLink *) malloc(sizeof(struct hashLink));
    hlnk->value = v;
    hlnk->key = k;
    hlnk->next = NULL;
    ht->table[idx] = hlnk;
    ht->count++;
}

else{
            plink = ht->table[idx];
            hlnk = (struct hashLink *) malloc(sizeof(struct hashLink));
            hlnk->value = v;
            hlnk->key = k;
            hlnk->next = plink->next;
            plink->next = hlnk;
            ht->count++;
    }
}