我无法弄清楚这个用c编写的链表代码的问题



所以我在主函数中声明了这个由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的函数调用以这种方式传递HashMap的地址:

for(x=0;x<sz;x++){
HMKey = Compute_HashMapKey_for_each_title(SearchTitle[x]);
//printf("%sn", 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[y]根据过程保存这些值中的每一个:

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({在第二次通过时消失,甚至退出调试器模式。

我在日志文件中只能看到这样的消息:[劣1(进程11288(退出,代码为030000000005]调试器已完成,状态为0

我在谷歌上搜索了错误消息,找不到任何有用的东西。

请允许我帮忙。提前感谢

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

相关内容

  • 没有找到相关文章