可视化解释反向链接列表数据结构代码所需的指导? [英] Visual Explanation Guidance needed for reversal of Linked List datastructure code?
问题描述
我有以下代码来反转链表。我在while循环中感到困惑,所以如果有人可以提供其实际工作的视觉解释,那么肯定会感激。
static void Reverse(struct node ** headRef)
{
struct node * result = NULL;
struct node * current = * headref;
struct node * next;
while(current!= NULL)
{
next = current-> next;
current-> next = result;
result = current;
current = next;
}
* headRef = result;
}
这是我试图让valya的答案更清楚(虽然我以为这已经很好了):
说我们有这个列表:
// a-> b-> c-> d-> e-> NULL
/ pre>
我们从第一个节点开始,
a
,其中包含一个指针($
b
b/ a-> b ...
行
next = current- < next;
setnext
tob
(够简单)。下一行current-> next = result;
执行此操作:// NULL< -ab ...(注意不再有从a到b的指针)
然后我们有
result = current;
将result
设置为a
(再次简单)。最后,我们将所有重要的current = next;
将当前
设置为b
。
所以在while循环的下一个迭代中,将
next
设置为b
,result
设置为a
,当前
设置为b
,我们重新开始:next = current-> next;
// NULL< -a&-b@cb
current-> next = result;
result = current;
然后我们再次执行:
next = current-> next;
// NULL< -a< -b< -c d ...
current-> next = result;
result = current;
一旦我们得到了链表中的最后一个项目(
e
在这个例子中),这发生:next = current-> next; // next变为NULL
// NULL< -a< -b< -c< -d< -e
current-> next =
result = current; // result is now e
current = next; // current is now NULL
现在,因为
当前
为NULL,while循环终止,我们留下:* headRef =结果;
你现在可以看到,使
headRef
指向e
,将e
作为我们的链接列表中的新的第一项,e-> next
指向d
,d-> next
指向c
等。I have following piece of code for reversing the linked list. I am getting confused in while loop and so would certainly appreciate if someone can provide visual explanation of how actually it's working.
static void Reverse (struct node** headRef) { struct node* result = NULL; struct node* current = *headref; struct node* next; while(current != NULL) { next = current->next; current->next = result; result = current; current = next; } *headRef = result; }
解决方案OK, here's my attempt to make valya's answer even clearer (though I thought it was pretty good already):
Say we have this list:
// a->b->c->d->e->NULL
We start at the first node,
a
, which contains a pointer (next
) tob
:// a->b ...
The line
next = current->next;
setsnext
tob
(simple enough). The next linecurrent->next = result;
does this:// NULL<-a b ... (notice there is no longer a pointer from a to b)
Then we have
result = current;
which setsresult
toa
(again, simple enough). And finally, we have the all importantcurrent = next;
, which setcurrent
tob
.So on the next iteration of the while loop, with
next
set tob
,result
set toa
, andcurrent
set tob
, we start over:next = current->next; // NULL<-a<-b c ... current->next = result; result = current;
Then we do it again:
next = current->next; // NULL<-a<-b<-c d ... current->next = result; result = current;
Once we've gotten to the last item in the linked list (
e
in this example), this happens:next = current->next; // next becomes NULL // NULL<-a<-b<-c<-d<-e current->next = result; result = current; // result is now e current = next; // current is now NULL
Now, since
current
is NULL, the while loop terminates, and we are left with:*headRef = result;
which, as you can see now, makes
headRef
point toe
, treatinge
as the new first item in our linked list, withe->next
pointing tod
,d->next
pointing toc
, etc.这篇关于可视化解释反向链接列表数据结构代码所需的指导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!