[英]Segmentation fault in a function to reverse a singly linked list recursivley
I am implementing a function to recursively reverse a linked-list, but getting seg-fault. 我正在实现一个递归反转链表的函数,但是遇到了seg-fault。
typedef struct _node {
int data;
struct _node *next;
} Node, *NodeP;
NodeP recursiveReverseList(NodeP first){
if(first == NULL) return NULL;
if(first->next == NULL) return first;
NodeP rest = recursiveReverseList(first->next);
rest->next = first;
first->next = NULL;
return first;
}
Can you please help? 你能帮忙吗?
PS The iterative version is working fine though. PS迭代版本工作正常。 Its not homework.
它不是功课。 Just practicing C.
只是练习C.
Thank you all :) 谢谢你们 :)
The general recursive algorithm for this is: 一般的递归算法是:
Divide
the list in 2
parts - first node and rest of the list. Divide
列表中的2
件-第一节点和列表的其余部分。 rest
of the linked list. rest
。 rest
to first
. rest
链接到first
。 head
pointer head
指针 You are doing steps 1 and 2 correctly but I guess you've messed up in steps 3 and 4. I would suggest you try this: 你正在正确地做第1步和第2步,但我猜你已经搞砸了第3步和第4步。我建议你试试这个:
NodeP recursiveReverseList(NodeP first){
if(first == NULL) return NULL; // list does not exist.
if(first->next == NULL) return first; // list with only one node.
NodeP rest = recursiveReverseList(first->next); // recursive call on rest.
//rest->next = first; CHANGE THIS
first->next->next = first; // make first next to the last node in the reversed rest.
first->next = NULL; // since first is the new last..make its next NULL.
//return first; CHANGE THIS
return rest; // rest now points to the head of the reversed list.
}
(source: geeksforgeeks.org ) (来源: geeksforgeeks.org )
. 。
EDIT: 编辑:
PS: I've not tested this. PS:我没试过这个。 So try it and let us know :) 所以尝试一下,让我们知道:)
I've tested the above function and seems to work as expected. 我测试了上面的功能,似乎按预期工作。 You can try the program here: http://ideone.com/bQXAV
你可以在这里试试这个程序: http : //ideone.com/bQXAV
@Unicornaddict has already posted a correct algorithm. @Unicornaddict已经发布了正确的算法。
But, if you are still getting segmentation fault
, I suspect you are making some mistake in calling the function from main
. 但是,如果你仍然遇到
segmentation fault
,我怀疑你在从main
调用函数时犯了一些错误。
head->next = recursiveReverseList(head->next);
head->next
to the recursive function. head->next
递归函数head->next
。 If you pass head
, it will do something like head
,它会做类似的事情 Before call:
来电前:
head ---> A ---> B ---> C头---> A ---> B ---> C.
After call:通话后:
head <--- A <--- B <--- C头<--- A <--- B <--- C.
which will make head
point to NULL
and A
point to head
这将使
head
指向NULL
和A
指向head
head->next
as argument, state of the list is: head->next
作为参数后,列表的状态为: head ---> A <--- B <--- C
头---> A <--- B <--- C.
So, you need to make head
point to rest
( C
in this case). 所以,你需要让
head
rest
(在这种情况下为C
)。
Your algorithm seems to be wrong. 你的算法似乎是错误的。 You need to return the pointer to the head of the new list, but you are returning the pointer to the last item.
您需要将指针返回到新列表的头部,但是您将指针返回到最后一个项目。
Indeed, you perhaps need both of them: a pointer to the head and the pointer to the last item. 实际上,您可能需要它们两个:指向头部的指针和指向最后一个项目的指针。
i think 我认为
rest->next = first;
should be 应该
first->next->next = first;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.