递归反转单链表的函数中的分段错误

发布于 2024-08-28 16:50:05 字数 448 浏览 4 评论 0原文

我正在实现一个函数来递归地反转链表,但出现段错误。

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;
}

你能帮忙吗?

PS 不过迭代版本运行良好。这不是家庭作业。刚刚练习C。

谢谢大家:)

I am implementing a function to recursively reverse a linked-list, but getting 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?

P.S. The iterative version is working fine though. Its not homework. Just practicing C.

Thank you all :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞳孔里扚悲伤 2024-09-04 16:50:05

一般的递归算法是:

  1. 将列表分为 2 部分 - 首先
    节点和列表的其余部分。
  2. 递归地调用reverse来获取剩余的
    链接列表。
  3. rest 链接到 first
  4. 修复 head 指针

您正确执行了步骤 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.
}

image
(来源:geeksforgeeks.org)

编辑:

PS:我还没有测试过这个。因此,请尝试一下并让我们知道:)

我已经测试了上述功能,并且似乎按预期工作。您可以在这里尝试该程序:
http://ideone.com/bQXAV

The general recursive algorithm for this is:

  1. Divide the list in 2 parts - first
    node and rest of the list.
  2. Recursively call reverse for the rest of the
    linked list.
  3. Link rest to first.
  4. Fix head pointer

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:

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.
}

image
(source: geeksforgeeks.org)
.

EDIT:

PS: I've not tested this. 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

月朦胧 2024-09-04 16:50:05

@Unicornaddict 已经发布了正确的算法。

但是,如果您仍然遇到分段错误,我怀疑您在从 main 调用函数时犯了一些错误。

正确:

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

解释:

  • head->next 传递给递归函数。如果你传递head,它会做类似的事情

通话前:
头 --->一个---> B---> C
通话后:
头<--- A <--- B <--- C

这将使 head 指向 NULLA 指向 head

  • head->next 作为参数传递后,列表的状态为:

头 ---> A <--- B <--- C

所以,你需要让 head 指向 rest (C在这种情况下)。

@Unicornaddict has already posted a correct algorithm.

But, if you are still getting segmentation fault, I suspect you are making some mistake in calling the function from main.

Correct:

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

Explanation:

  • Pass head->next to the recursive function. If you pass head, it will do something like

Before call:
head ---> A ---> B ---> C
After call:
head <--- A <--- B <--- C

which will make head point to NULL and A point to head

  • After passing head->next as argument, state of the list is:

head ---> A <--- B <--- C

So, you need to make head point to rest (C in this case).

还给你自由 2024-09-04 16:50:05

你的算法似乎是错误的。您需要将指针返回到新列表的头部,但您正在将指针返回到最后一项。

事实上,您可能需要它们两者:指向头部的指针和指向最后一项的指针。

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.

半寸时光 2024-09-04 16:50:05

我认为

rest->next = first;

应该是

first->next->next = first;

i think

rest->next = first;

should be

first->next->next = first;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文