简体   繁体   English

函数中的分段错误以反转单链表recursivley

[英]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: 一般的递归算法是:

  1. Divide the list in 2 parts - first node and rest of the list. Divide列表中的2件-第一节点和列表的其余部分。
  2. Recursively call reverse for the rest of the linked list. 递归调用反向链接列表的rest
  3. Link rest to first . rest链接到first
  4. Fix 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调用函数时犯了一些错误。

Correct: 正确:

head->next = recursiveReverseList(head->next);

Explanation: 说明:

  • Pass 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指向NULLA指向head

  • After passing 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM