[英]How can I solve the infinitely recursion run problem?
问题是要问两个用户之间的get连接度(每个用户都有一个好友列表)。 图片中给出了说明和示例。 下面是我的代码,此代码的工作度为 2 或更少,但连接更大或没有连接,它会无限运行。 我想知道这里的错误是什么。 '''
int get_degrees_of_connection(User *a, User *b)
{
if(strcmp(a->name,b->name)==0){
return 0;
}
if(in_friend_list(a->friends,b)){
return 1;
}
FriendNode *temp = NULL;
temp = a->friends;
User *current = NULL;
a->visited = true;
while(temp!=NULL){
current = temp->user;
if(current->visited == true){
continue;
}
int val = get_degrees_of_connection(current,b);
if(val!=-1){
return 1+val;
}
temp = temp->next;
}
return -1;
}
这个:
while(temp!=NULL){
current = temp->user;
if(current->visited == true){
continue;
}
当current->visited == true
时将永远运行。 在 continue 语句之前没有更新temp
的内容。 这就是为什么它将永远运行。
如果我不得不猜测,您可能希望在图遍历中遇到循环时调用break
not continue
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.