【问题标题】:I can't figure out the issue with this linked list code written with c我无法弄清楚这个用 c 编写的链表代码的问题
【发布时间】:2021-08-03 01:56:19
【问题描述】:

所以我已经在主函数中声明了这个由 9 个单链表节点组成的开放散列(或单独链接)数组,最初初始化为 NULL:

//Initialize HashMap array
struct node *HashMap[9];
int HM_indx = 0;
for(HM_indx=0;HM_indx<9;HM_indx++)
    HashMap[HM_indx] = NULL;

这是在主函数之外而不是在任何函数内部定义节点结构的方式:

struct node
{
    char *data;
    struct node *next;
};

然后一个函数调用:Insert_Title_To_HashMap_Using_HashMapKey 从main函数中传递HashMap的地址:

for(x=0;x<sz;x++){
    HMKey = Compute_HashMapKey_for_each_title(SearchTitle[x]);
    //printf("%s\n", HMKey);
    Insert_Title_To_HashMap_Using_HashMapKey(SearchTitle[x], HMKey, HashMap);
}

当然 SearchTitle[x] 和 HMkey 也会被传递。 HMKey 保存从哈希函数返回的 0-9 范围内的整数值:Compute_HashMapKey_for_each_title(SearchTitle[x]) 和 SearchTitle[x] 保存这些值中的每一个,具体取决于传递:

char *SearchTitle[6] = {"duel","dule","speed","spede","deul","cars"};

现在 Insert_Title_To_HashMap_Using_HashMapKey 是这样定义的:

void Insert_Title_To_HashMap_Using_HashMapKey(char title[], char *HashMapKey, char *HM){
    int LDHMKeyACodeSum;
    size_t title_len = strlen(title);
    LDHMKeyACodeSum = Get_Last_Digit_Of_Sum_Of_Ascii_Equivalent_For_Each_HashMapKey_Character(HashMapKey);
    //Initialize newnode
    struct node *newnode = malloc(sizeof(struct node));
    //Assign title to newnode value
    newnode->data = (char*)malloc(title_len + 1);
    strncpy(newnode->data, title, title_len);
    //Assign Null value to link part of newnode
    newnode->next = NULL;
    if(HM[LDHMKeyACodeSum] == NULL){//if the headnode of the particular node where we are to insert title is NULL
        HM[LDHMKeyACodeSum] = newnode; //make newnode HeadNode
    }else{
        //Initialize tempnode for traversing the list
        //struct node *tempnode = malloc(sizeof(struct node));
        struct node *tempnode = HM[LDHMKeyACodeSum]; //point tempnode to HeadNode
        while(tempnode->next != NULL){
            tempnode = tempnode->next; //move to next tempnode link part
        }//End While
        tempnode->next = newnode; //point tempnode link part to newnode
    }//end if
}

完整代码中的其他所有内容都按预期工作,但我无法弄清楚为什么代码会到达这一行:while(tempnode->next != NULL){ 在第二遍时消失甚至退出调试器模式。

我在日志文件中只能看到这条消息:[Inferior 1 (process 11288) exited with code 030000000005] 调试器以状态 0 完成

我用谷歌搜索了错误信息,但找不到任何有用的信息。

如果有任何帮助,我将不胜感激。提前致谢

【问题讨论】:

  • char *HMchar pointer 而传递的参数 HashMapstruct node*
  • @TortelliniTeusday 哈哈哈,当你开始对函数和变量命名过于合乎逻辑时会发生这种情况 XD
  • @anirudh 非常感谢您发现 HM 是 char 而不是 struct node 的指针。这惩罚了我几个小时,我什至不知道为什么我没有发现它。非常感谢。我希望你把它作为答案,我应该投票给它。
  • 我从字面上理解...哈哈哈@eedideyahoocom

标签: c linked-list


【解决方案1】:

char *HM 是一个字符指针,而传递的参数 HashMap 是一个struct node*

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2013-11-21
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多