【发布时间】:2014-07-14 03:57:30
【问题描述】:
我遇到了以下使用递归来反转堆栈的函数。对它的工作原理感到困惑。
请帮助我以更简单的方式理解。
void stack_reverse(struct node **head, struct node **head_next)
{
struct node *temp;
if (*head_next != NULL)
{
temp = (*head_next)->next;
(*head_next)->next = (*head);
*head = *head_next;
*head_next = temp;
stack_reverse(head, head_next);
}
}
【问题讨论】:
-
不值得弄清楚它是如何工作的。这是糟糕的、过于复杂的代码。
标签: c recursion data-structures linked-list tail-recursion