Algorithm 颠倒链表数据结构代码所需的直观解释指南?
我有下面一段代码来反转链表。我在while循环中感到困惑,因此如果有人能提供它实际工作原理的视觉解释,我将不胜感激Algorithm 颠倒链表数据结构代码所需的直观解释指南?,algorithm,data-structures,linked-list,Algorithm,Data Structures,Linked List,我有下面一段代码来反转链表。我在while循环中感到困惑,因此如果有人能提供它实际工作原理的视觉解释,我将不胜感激 static void Reverse (struct node** headRef) { struct node* result = NULL; struct node* current = *headref; struct node* next; while(current != NULL) { next =
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;
}
列表如下所示:
=>(1) => => => => => => => => =>(10)
我们把名单上的每一项都颠倒了
<=(1) => => => => => => => => =>(10)
<=(1) <= => => => => => => => =>(10)
<=(1) <= <= => => => => => => =>(10)
...
<=(1) <= <= <= <= <= <= <= <= <=(10)
查看网站以获得视觉表现。这里看起来也有一个很好的代码项目解释(见技术3)。我用dot做了一个图表,我认为它可以以图形方式解释发生了什么: 下面是(不整洁的)点源,以防有人在意:
digraph g {
label = "Start"
subgraph cluster_1
{
a1 -> b1 -> c1;
current1 -> a1;
result1
a1 [label="a"]
b1 [label="b"]
c1 [label="c"]
current1 [label="current"]
result1 [label="result"]
}
label = "Once through loop"
subgraph cluster_2
{
current2 -> b2;
result2 -> a2;
b2 -> c2;
a2 [label="a"]
b2 [label="b"]
c2 [label="c"]
current2 [label="current"]
result2 [label="result"]
}
label = "Twice through loop"
subgraph cluster_3
{
current3 -> c3;
result3 -> b3;
b3 -> a3;
a3 [label="a"]
b3 [label="b"]
c3 [label="c"]
current3 [label="current"]
result3 [label="result"]
}
label = "Final"
subgraph cluster_4
{
result4 -> c4 -> b4 -> a4;
a4 [label="a"]
b4 [label="b"]
c4 [label="c"]
current4 [label="current"]
result4 [label="result"]
}
label = ""
}
好的,下面是我试图让valya的答案更清楚(尽管我认为它已经很好了): 假设我们有以下列表:
// a->b->c->d->e->NULL
我们从第一个节点开始,a
,它包含一个指向b
的指针(next
):
// a->b ...
行next=current->next代码>将next
设置为b
(非常简单)。下一行current->next=result代码>这样做:
// NULL<-a b ... (notice there is no longer a pointer from a to b)
然后我们再做一次:
next = current->next;
// NULL<-a<-b<-c d ...
current->next = result;
result = current;
现在,由于current
为空,while循环终止,剩下的是:
*headRef = result;
正如您现在看到的,这使得headRef
指向e
,将e
视为我们链接列表中的第一个新项目,其中e->next
指向d
,d->next
指向c
,等等。请参阅以获得良好的解释。以下是摘录:
public class List {
private class Node {
int data;
Node next;
}
private Node head;
public void reverse() {
if (head == null) return;
Node prev = null,curr = head,next = null;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
}
The list:
==========
1->2->3->4->5
------------------------------------------------------------------
Running of the code:
============================
At start:
**********
head = 1;
prev = null,curr = 1, next = null
1st iteration of while loop:
******************************
next = 2;
2->3->4->5
curr.next = null;
1->null
prev = 1;
1->null
curr = 2;
2->3->4->5
2nd iteration of while loop:
******************************
next = 3
3->4->5
curr.next = 1
2->1->null
prev = 2
2->1->null
curr = 3
3->4->5
3rd iteration of while loop:
******************************
next = 4
4->5
curr.next = 2
3->2->1->null
prev = 3
3->2->1->null
curr = 4
4->5
4th iteration of while loop:
******************************
next = 5
5->null
curr.next = 3
4->3->2->1->null
prev = 4
4->3->2->1->null
curr = 5
5->null
5th iteration of while loop:
******************************
next = null
null
curr.next = 4
5->4->3->2->1->null
prev = 5
5->4->3->2->1->null
curr = null
There is no 6th iteration and the while loop terminates since
curr is null and the check is for curr != null.
last statement:
==================
head = prev
This statement leaves the list reversed with referencing head with the reversed node prev
to become the new head node.
你能提供图表说明每一步的实际情况吗?检查另一条评论中的链接:是的,我的答案给出了一个直观的表示这更有帮助。难怪他们说一张图片胜过千言万语你的第一个链接断了
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
*headRef = result;
public class List {
private class Node {
int data;
Node next;
}
private Node head;
public void reverse() {
if (head == null) return;
Node prev = null,curr = head,next = null;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
}
The list:
==========
1->2->3->4->5
------------------------------------------------------------------
Running of the code:
============================
At start:
**********
head = 1;
prev = null,curr = 1, next = null
1st iteration of while loop:
******************************
next = 2;
2->3->4->5
curr.next = null;
1->null
prev = 1;
1->null
curr = 2;
2->3->4->5
2nd iteration of while loop:
******************************
next = 3
3->4->5
curr.next = 1
2->1->null
prev = 2
2->1->null
curr = 3
3->4->5
3rd iteration of while loop:
******************************
next = 4
4->5
curr.next = 2
3->2->1->null
prev = 3
3->2->1->null
curr = 4
4->5
4th iteration of while loop:
******************************
next = 5
5->null
curr.next = 3
4->3->2->1->null
prev = 4
4->3->2->1->null
curr = 5
5->null
5th iteration of while loop:
******************************
next = null
null
curr.next = 4
5->4->3->2->1->null
prev = 5
5->4->3->2->1->null
curr = null
There is no 6th iteration and the while loop terminates since
curr is null and the check is for curr != null.
last statement:
==================
head = prev
This statement leaves the list reversed with referencing head with the reversed node prev
to become the new head node.