[英]How can I get this recursive linked list reverse work?
I tried to write a recursive linked list reverse function and my head is spinning. 我试图编写一个递归链表反向函数,但我的头在旋转。 Can someone please tell me how can I mend the function?
有人可以告诉我如何修改此功能吗?
node* recursiveReverse(node* h, node* prev)
{
node* current = h; //Point to current node
node* successor = h->next; //Points to next node
if(successor == NULL)
return prev;
successor->next = recursiveReverse(successor,current);
h->next = NULL;
return successor;
}
if(successor = NULL) // <---
return prev;
Should be ==
here. 应该在这里
==
。 ( =
is assignment, ==
is compare for equality). (
=
是分配, ==
是比较是否相等)。
It is a common problem and most compilers should have issued a warning for this. 这是一个常见问题,大多数编译器都应该为此发出警告。 If you haven't turned on warnings (add the
-Wall
flag if you're using gcc or clang), please do so. 如果您尚未打开警告(如果使用的是gcc或clang,请添加
-Wall
标志),请这样做。
You should also try to write the constants first to avoid such problems. 您还应该尝试首先编写常量以避免此类问题。 For instance: If you write
例如:如果您写
if ( NULL == successor )
return prev;
and you now forget to write two "=", the compiler throws an error because you can't assign NULL a value. 现在您忘了写两个“ =”,编译器将引发错误,因为您无法为NULL赋值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.