基本上我这里有两个链接列表:列表和不同的。有几组单词之前已保存到“列表”结构中。我要编写一个程序,它会找到不同/唯一的单词并将其保存到“不同”结构中。这是我到目前为止根据我的指针概念得到的结果。然而,当我尝试打印“distinct”时,程序崩溃:(如果我错了,请纠正我。
struct list {
char string[50];
struct list *next;
};
struct distinct {
char string[50];
struct distinct *next;
};
void checkdistinct() {
list *ori = NULL;
distinct *copy = NULL;
distinct *check = NULL;
if(ori == NULL && copy == NULL) { //first time.
ori = ori->next;
copy = copy->next;
copy = (distinct*)malloc(sizeof(distinct));
strcpy(copy->string, ori->string);
ori = ori->next;
copy = copy->next;
}
else {}
while(ori!=NULL) {
check = check->next;
while(check != NULL) {
if(strcmp(ori->string, check->string)!=0) {
check = check->next;
}
else {
ori = ori->next;
check = NULL;
}
}
//only compare same casing words, for now.
copy = (distinct*)malloc(sizeof(distinct));
strcpy(copy->string, ori->string);
ori = ori->next;
copy = copy->next;
}
}
当我尝试在 main 中打印时,它会崩溃:(如果您需要代码的额外注释,请回复。谢谢!
这三行可能是罪魁祸首之一:
if(ori == NULL && copy == NULL) { //first time.
ori = ori->next;
copy = copy->next;
在这里你检查是否ori
and copy
are NULL
, 然后你立即地取消引用那些NULL
指点一下!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)